﻿module Sustainalytics.Screening {
    export class EditFilterCriteriaController {
        public typeFourSelectedCategories: any;
        public selectedCriteria: SelectedCriteriaDto;
        public selectedFilterCriteria: SelectedCriteriaDto;
        public tool: any;
        public tabs: CriteriaTabs[];
        public searchText: string;
        public templateCriteriaForLevel3: TemplateCriteriaDto[]
        public baseCriteriaList: BaseCriteria[];
        public currentCriteriaLevel3Id: string;

        public criteriaTypeOneChart: any;
        public criteriaTypeSixChart: any;
        public criteriaType16Chart: any;

        public currentCriteriaTypeOne: CriteriaTypeOne;
        public currentCriteriaTypeThree: CriteriaTypeThree;
        public currentCriteriaTypeSix: CriteriaTypeSix;
        public currentCriteriaTypeSeven: CriteriaTypeSeven;
        public currentCriteriaType16: CriteriaType16;

        public gcSelectedItems: string;

        public listSelectedFilterCriteria: any;

        //for select all feature from Reports
        public isSelectAllChecked: boolean;
        public listChecks: any;
        public isRiskRating: boolean;
        public criteriaTypeByCriteriaLevel3: number;

        public selectedChildTab: any;
        public oneAtATime: any;
        public showCriteriaLevel2: boolean;
        public showCriteriaLevel3: boolean;
        public showTemplateCriteria: boolean;

        public isSelectAllDisabled: boolean;

        public currentSelection: string;

        constructor(
            private $rootScope: ng.IScope,
            private $scope: ng.IScope,
            private $q: ng.IQService,
            private $location: ng.ILocationService,
            private notificationFactory: Common.INotificationFactory,
            private staticObjects: Common.IStaticObjects,
            private screeningToolFactory: IScreeningToolFactory,
            private templateCriteriaService: ITemplateCriteriaService,
            private filterCriteriaService: IFilterCriteriaService,
            private $modal: Common.IModal,
            private $modalInstance: Common.IModalInstance,
            public filterId: string,
            public filterCriteriaId: string,
            public isReport: boolean,
            private claimsAuthorizationService: Claims.ClaimsAuthorizationService) {
            var ctx = this;
            $scope['editCriteria'] = ctx;
            ctx.initSelectedCriteria();

            ctx.baseCriteriaList = [];
            ctx.listSelectedFilterCriteria = [];
            ctx.listChecks = [];

            ctx.tool = { template: 'Views/Tools/ScreeningTool/FilterCriteria/criteriaTabs.html' };

            ctx.initTabs();
            ctx.oneAtATime = true;
            ctx.showCriteriaLevel2 = true;
            ctx.showCriteriaLevel3 = false;
            ctx.showTemplateCriteria = false;
        }

        public initSelectedCriteria() {
            var ctx = this;

            ctx.selectedCriteria = {
                id: null,
                name: null,
                criteriaLevel3Id: null,
                type: null,
                filterGroupId: null,
                loading: false,
                isUpdating: false,
                companyCount: undefined,

                isScreenable: null,

                typeOneMinValue: null,
                typeOneMaxValue: null,
                typeOneIsNotScreenableCriteriaItem: null,

                typeTwoIsScreenableCriteriaItems: [],
                typeTwoIsNotScreenableCriteriaItems: [],

                typeThreeCollectionName: "",
                typeThreeSelectedItems: [],                
                typeThreeIsScreenableCriteriaItems: [],
                typeThreeIsNotScreenableCriteriaItems : [],

                typeFourCategories: [],
                typeFourSymbol: "",
                typeFourScreenOnAnyInvolvement: false,
                typeFourIsNotScreenableCriteriaItems: [],

                typeFiveIsScreenableCriteriaItems: [],
                typeFiveIsNotScreenableCriteriaItems: [],

                typeSixMinValue: 0,
                typeSixMaxValue: 0,
                typeSixIsNotScreenableCriteriaItems: [],

                collectionItemsTypeSeven: ['0', '1', '2'],
                typeSevenSelectedItems: [],
                typeSevenIsScreenableCriteriaItems: [],
                typeSevenIsNotScreenableCriteriaItems: [],

                typeEightIsScreenableCriteriaItems: [],
                typeEightIsNotScreenableCriteriaItems: [],

                typeNineIsScreenableCriteriaItems: [],
                typeNineIsNotScreenableCriteriaItems: [],

                typeTenIsScreenableCriteriaItems: [],
                typeTenIsNotScreenableCriteriaItems: [],

                type16MinValue: null,
                type16MaxValue: null,
                type16IsNotScreenableCriteriaItems: [],

                type17IsScreenableCriteriaItems: [],
                type17IsNotScreenableCriteriaItems: [],

                type25IsScreenableCriteriaItems: [],
                type25IsNotScreenableCriteriaItems: []
            };
        }

        //#region Tabs load
        public initTabs() {
            var ctx = this;
            var promiseLoadBaseCriteria = ctx.loadBaseCriteriaList();
            var promiseGetCriteriaTabs = ctx.getCriteriaTabsAndTemplates();

            // after get and count are resolved set controller properties
            var promiseAll = ctx.$q.all([promiseGetCriteriaTabs]);

            promiseAll
                .then(function () {
                //load filterCriteria
                if (ctx.filterCriteriaId != null) {
                    ctx.getSelectedCriteriaByFilterCriteriaId(ctx.filterCriteriaId);
                }
            });
        }

        public loadBaseCriteriaList = function () {
            var ctx = this;
            ctx.$rootScope['screeningExportDetails'] = new ExportDetailsDto();
            var promise = ctx.screeningToolFactory.listBaseCriteria().then(data => {
                ctx.baseCriteriaList.length = 0;
                angular.forEach(data, baseCriteria => {
                    ctx.baseCriteriaList.push(baseCriteria);
                })
            }, error => {
                    //notificationFactory.error("Error", staticObjects.getErrorMesageByStatusCode(status, data));
                });
            return promise;
        }

        public getCriteriaTabsAndTemplates = function () {
            var ctx = this;
            var promise = ctx.screeningToolFactory.getCriteriaTabsAndTemplates()
                .then(criteriaTabs => {
                ctx.tabs = criteriaTabs;

                for (var l1 = 0; l1 < criteriaTabs.length; l1++) {
                    var level1 = criteriaTabs[l1].childItems;
                    for (var l2 = 0; l2 < level1.length; l2++) {
                        var level2 = level1[l2].baseCriteria;
                        var newBaseCriteria = [];
                        var categories = {};
                        for (var l3 = 0; l3 < level2.length; l3++) {
                            var level3 = level2[l3];

                            if (!(level3.category in categories)) {
                                categories[level3.category] = { category: level3.category, baseCriteria: [] };
                                newBaseCriteria.push(categories[level3.category]);
                            }
                            if (level3.name === "Global Compact Compliance")
                                categories[level3.category].baseCriteria.push({ id: level3.id, name: level3.name, isEnabled: ctx.validateCriteriaForGccs() });
                            else if (level3.name.toLowerCase() === "sedol" || level3.name.toLowerCase() === "wkn" || level3.name.toLowerCase() === "valor") {
                                categories[level3.category].baseCriteria.push({ id: level3.id, name: level3.name, isEnabled: ctx.validateCriteria(level3.name.toLowerCase())});
                            }
                            else if (level1[l2].shortName.toLowerCase() === "pi") {
                                categories[level3.category].baseCriteria.push({ id: level3.id, name: level3.name, isEnabled: ctx.validateCriteriaForPi(level3.name) });
                            }
                            else {
                                categories[level3.category].baseCriteria.push({ id: level3.id, name: level3.name, isEnabled: ctx.validateCriteria(level1[l2].shortName.toLowerCase()) });
                            }
                        }

                        level1[l2].baseCriteria = newBaseCriteria;
                    }
                }

                ctx.tabs = criteriaTabs;
            }, error => {
                    //notificationFactory.error("Error", staticObjects.getErrorMesageByStatusCode(status, data));
                });
            return promise;
        }
        //#endregion

        public scoreIsSelected(score, criteriaType): boolean {
            var ret = false;
            if (criteriaType == 9)
                ret = (score > 0 && score != "0" && score != "Off");
            else
                ret = (score != -1 && score != "Off");
            return ret;
        }

        //Controls  the "add criteria" button state (enabled or disabled)
        public isSelectionValid() {
            var ctx = this;

            switch (ctx.selectedCriteria.type) {
                case 1:
                    if (ctx.selectedCriteria.typeOneIsNotScreenableCriteriaItem != null) {
                        if (ctx.selectedCriteria.typeOneIsNotScreenableCriteriaItem.isOn)
                            return true;
                    }
                    if (ctx.criteriaTypeOneChart)
                        return !ctx.criteriaTypeOneChart.invalidInput();
                    break;
                case 2:
                    for (var i = 0; i < ctx.selectedCriteria.typeTwoIsScreenableCriteriaItems.length; i++) {
                        if (ctx.selectedCriteria.typeTwoIsScreenableCriteriaItems[i].score != 0) {
                            return true;
                        }
                    }
                    for (var i = 0; i < ctx.selectedCriteria.typeTwoIsNotScreenableCriteriaItems.length; i++) {
                        if (ctx.selectedCriteria.typeTwoIsNotScreenableCriteriaItems[i].isOn) {
                            return true;
                        }
                    }
                    break;
                case 3:                   
                    if (ctx.selectedCriteria.typeThreeIsNotScreenableCriteriaItems) {
                        for (var i = 0; i < ctx.selectedCriteria.typeThreeIsNotScreenableCriteriaItems.length; i++) {
                            if (ctx.selectedCriteria.typeThreeIsNotScreenableCriteriaItems[i].isOn)
                                return true;                            
                        }
                    }
                    if (ctx.selectedCriteria != null && ctx.selectedCriteria.typeThreeSelectedItems != null)
                        if (ctx.selectedCriteria.typeThreeSelectedItems.length > 0)
                            return true;
                    break;
                case 4:
                    if (ctx.typeFourHasAnyInvolvement(ctx.selectedCriteria))
                        return true;

                    for (var i = 0; i < ctx.selectedCriteria.typeFourIsNotScreenableCriteriaItems.length; i++) {
                        if (ctx.selectedCriteria.typeFourIsNotScreenableCriteriaItems[i].isOn) {
                            return true;
                        }
                    }
                    break;
                case 5:
                    for (var i = 0; i < ctx.selectedCriteria.typeFiveIsScreenableCriteriaItems.length; i++) {
                        if (ctx.scoreIsSelected(ctx.selectedCriteria.typeFiveIsScreenableCriteriaItems[i].score, 5)) {
                            return true;
                        }
                    }
                    for (var i = 0; i < ctx.selectedCriteria.typeFiveIsNotScreenableCriteriaItems.length; i++) {
                        if (ctx.selectedCriteria.typeFiveIsNotScreenableCriteriaItems[i].isOn) {
                            return true;
                        }
                    }
                    break;
                case 6:
                    if (ctx.criteriaTypeSixChart && !ctx.criteriaTypeSixChart.invalidInput())
                        return true;
                    for (var i = 0; i < ctx.selectedCriteria.typeSixIsNotScreenableCriteriaItems.length; i++) {
                        if (ctx.selectedCriteria.typeSixIsNotScreenableCriteriaItems[i].isOn) {
                            return true;
                        }
                    }
                    break;
                case 7:
                    if (ctx.selectedCriteria != null && ctx.selectedCriteria.typeSevenSelectedItems != null) {
                        if (ctx.selectedCriteria.typeSevenSelectedItems.length > 0) {
                            return true;
                        }
                    }
                    if (ctx.selectedCriteria != null && ctx.selectedCriteria.typeSevenIsNotScreenableCriteriaItems != null) {
                        for (var i = 0; i < ctx.selectedCriteria.typeSevenIsNotScreenableCriteriaItems.length; i++) {
                            if (ctx.selectedCriteria.typeSevenIsNotScreenableCriteriaItems[i].isOn) {
                                return true;
                            }
                        }
                    }
                    break;
                case 8:
                    for (var i = 0; i < ctx.selectedCriteria.typeEightIsScreenableCriteriaItems.length; i++) {
                        if (ctx.selectedCriteria.typeEightIsScreenableCriteriaItems[i].score != 0) {
                            return true;
                        }
                    }
                    for (var i = 0; i < ctx.selectedCriteria.typeEightIsNotScreenableCriteriaItems.length; i++) {
                        if (ctx.selectedCriteria.typeEightIsNotScreenableCriteriaItems[i].isOn) {
                            return true;
                        }
                    }
                    break;
                case 9:
                    for (var i = 0; i < ctx.selectedCriteria.typeNineIsScreenableCriteriaItems.length; i++) {
                        if (ctx.scoreIsSelected(ctx.selectedCriteria.typeNineIsScreenableCriteriaItems[i].score, 9)) {
                            return true;
                        }
                    }
                    for (var i = 0; i < ctx.selectedCriteria.typeNineIsNotScreenableCriteriaItems.length; i++) {
                        if (ctx.selectedCriteria.typeNineIsNotScreenableCriteriaItems[i].isOn) {
                            return true;
                        }
                    }
                    break;
                case 10:
                    for (var i = 0; i < ctx.selectedCriteria.typeTenIsNotScreenableCriteriaItems.length; i++) {
                        if (ctx.selectedCriteria.typeTenIsNotScreenableCriteriaItems[i].isOn) {
                            return true;
                        }
                    }
                    break;
                case 16:
                    if (ctx.criteriaType16Chart && !ctx.criteriaType16Chart.invalidInput())
                        return true;
                    for (var i = 0; i < ctx.selectedCriteria.type16IsNotScreenableCriteriaItems.length; i++) {
                        if (ctx.selectedCriteria.type16IsNotScreenableCriteriaItems[i].isOn) {
                            return true;
                        }
                    }
                    break;
                case 17:
                    for (var i = 0; i < ctx.selectedCriteria.type17IsScreenableCriteriaItems.length; i++) {
                        if (ctx.scoreIsSelected(ctx.selectedCriteria.type17IsScreenableCriteriaItems[i].score, 17)) {
                            return true;
                        }
                    }
                    for (var i = 0; i < ctx.selectedCriteria.type17IsNotScreenableCriteriaItems.length; i++) {
                        if (ctx.selectedCriteria.type17IsNotScreenableCriteriaItems[i].isOn) {
                            return true;
                        }
                    }
                    break;
                case 25:
                    for (var i = 0; i < ctx.selectedCriteria.type25IsScreenableCriteriaItems.length; i++) {
                        if (ctx.scoreIsSelected(ctx.selectedCriteria.type25IsScreenableCriteriaItems[i].score, 25)) {
                            return true;
                        }
                    }
                    for (var i = 0; i < ctx.selectedCriteria.type25IsNotScreenableCriteriaItems.length; i++) {
                        if (ctx.selectedCriteria.type25IsNotScreenableCriteriaItems[i].isOn) {
                            return true;
                        }
                    }
                    break;
            }

            return false;
        }

        public closeDialog = function (refresh: boolean) {
            var ctx = this;
            ctx.$modalInstance.close({ refresh: refresh });
        }

        //#region Search
        public baseCriteriaSelected = (baseCriteria: BaseCriteria) => {
            var ctx = this;

            var bCriteria = ctx.activateTabForBaseCriteriaName(baseCriteria.criteriaLevel3Id);

            // bCriteria is null only when the selected TemplateCriteria has a non-existing CriteriaLevel3Id (baseCriteria["criteriaLevel3Name"] = string.Empty)
            if (bCriteria != null) {
                ctx.selectCriteria(bCriteria);
                ctx.listTemplateCriteriaByLevel3Id(bCriteria);
            }

            ctx.selectedCriteria.loading = true;

            ctx.searchText = "";

            $(".portfolio-search-results").addClass("ng-hide");
        }

        public activateTabForBaseCriteriaName = function (criteriaLevel3Id: string): BaseCriteria {
            var ctx = this;

            // deactivate (remove the 'active' class) all childs first
            for (var i = 0; i < ctx.tabs.length; i++) {
                var parentTab = ctx.tabs[i];
                parentTab.active = false;

                for (var j = 0; j < parentTab.childItems.length; j++) {
                    var childTab = parentTab.childItems[j];
                    childTab.active = false;
                }
            }

            for (var i = 0; i < ctx.tabs.length; i++) {
                var parentTab = ctx.tabs[i];

                for (var j = 0; j < parentTab.childItems.length; j++) {
                    var childTab = parentTab.childItems[j];

                    for (var w = 0; w < childTab.baseCriteria.length; w++) {
                        // CriteriaLevel2
                        var baseCriteria = childTab.baseCriteria[w];

                        // foreach CriteriaLevel3
                        for (var x = 0; x < baseCriteria.baseCriteria.length; x++) {
                            var criteriaLevel3 = baseCriteria.baseCriteria[x];

                            if (criteriaLevel3Id === criteriaLevel3.id) {
                                parentTab.active = true;
                                childTab.active = true;
                                ctx.selectedChildTab = childTab;
                                return criteriaLevel3;
                            }
                        }
                    }
                }
            }
            return null;
        };

        public activateTabForChildTabName = function (name: string) {
            var ctx = this;

            for (var i = 0; i < ctx.tabs.length; i++) {
                var parentTab = ctx.tabs[i];
                for (var j = 0; j < parentTab.childItems.length; j++) {
                    var childTab = parentTab.childItems[j];
                    if (childTab.name == name) {
                        parentTab.active = true;
                        childTab.active = true;
                    }
                    else {
                        childTab.active = false;
                    }
                }
            }
        };
        //#endregion

        //used to activate the current tab
        public activateBaseCriteria = function (name: string) {
            var ctx = this;
            for (var i = 0; i < ctx.tabs.length; i++) {
                var parentTab = ctx.tabs[i];
                for (var j = 0; j < parentTab.childItems.length; j++) {
                    var childTab = parentTab.childItems[j];
                    for (var w = 0; w < childTab.baseCriteria.length; w++) {
                        var baseCriteria = childTab.baseCriteria[w];
                        baseCriteria.isActive = baseCriteria.name == name;
                    }
                }
            }
        };

        //#region Tab changed

        //add enabled/disabled class for tab
        public getTemplateCriteriaClass = function (isActive, isEnabled) {
            var className = "";

            if (isActive)
                className = "active";

            if (!isEnabled && isEnabled != undefined)
                className = "disabled";

            return className;
        };

        //#endregion

        //used for listing the template criteria with the same CriteriaLevel3Id
        public listTemplateCriteriaByLevel3Id(baseCriteria: BaseCriteria) {
            var ctx = this;

            ctx.currentCriteriaTypeOne = null;
            ctx.currentCriteriaTypeThree = null;
            ctx.currentCriteriaTypeSix = null;
            ctx.currentCriteriaTypeSeven = null;
            ctx.currentCriteriaType16 = null;
            ctx.currentCriteriaLevel3Id = baseCriteria.id;
            ctx.selectedCriteria.name = baseCriteria.name;

            ctx.templateCriteriaService.listTemplateCriteriaByLevel3Id(ctx.currentCriteriaLevel3Id).then(templateCriteriaDto => {
                var criteriaType = templateCriteriaDto["data"][0].criteriaType;
                ctx.templateCriteriaForLevel3 = null;
                //if we have only one TemplateCriteria it will we selected by default (this is implemented for CriteriaType 1, 3, 6)
                switch (criteriaType) {
                    case 1:
                        if (!ctx.isReport && templateCriteriaDto["data"][0].isScreenable) {
                            if (templateCriteriaDto["data"].length == 1) {
                                var firstTemplateCriteria = templateCriteriaDto["data"][0];
                                ctx.selectedCriteria.isScreenable = true;
                                ctx.selectTemplateCriteria(firstTemplateCriteria);
                            }
                            else {
                                ctx.templateCriteriaForLevel3 = templateCriteriaDto["data"];
                            }
                        }
                        else {
                            var dataPoint = {
                                id: templateCriteriaDto["data"][0].id,
                                name: templateCriteriaDto["data"][0].name,
                                isDataPoint: true,
                                isOn: false
                            }
                            ctx.selectedCriteria.typeOneIsNotScreenableCriteriaItem = dataPoint;
                            ctx.selectedCriteria.isScreenable = false;
                            ctx.selectTemplateCriteria(templateCriteriaDto["data"][0]);
                        }
                        break;
                    case 2:
                        ctx.selectCriteriaType2(templateCriteriaDto["data"]);
                        break;
                    case 3:
                        ctx.selectCriteriaType3(templateCriteriaDto["data"]);                       
                        break;
                    case 4:
                        ctx.selectCriteriaType4(templateCriteriaDto["data"]);
                        break;
                    case 5:
                        ctx.selectCriteriaType5(templateCriteriaDto["data"]);
                        break;
                    case 6:
                        ctx.selectCriteriaTypeSix(templateCriteriaDto["data"]);
                        break;
                    case 7:
                        ctx.selectedCriteria.typeSevenSelectedItems = new Array();
                        ctx.selectCriteriaType7(templateCriteriaDto["data"]);
                        break;
                    case 8:
                        ctx.selectCriteriaType8(templateCriteriaDto["data"]);
                        break;
                    case 9:
                        ctx.selectCriteriaType9(templateCriteriaDto["data"]);
                        break;
                    case 10:
                        ctx.selectCriteriaType10(templateCriteriaDto["data"]);
                        break;
                    case 16:
                        ctx.selectCriteriaType16(templateCriteriaDto["data"]);
                        break;
                    case 17:
                        ctx.selectCriteriaType17(templateCriteriaDto["data"]);
                        break;
                    case 25:
                        ctx.selectCriteriaType25(templateCriteriaDto["data"]);
                        break;
                    default:
                        ctx.templateCriteriaForLevel3 = templateCriteriaDto["data"];
                        break;
                }
            });
            ctx.showTemplateCriteria = true;
            ctx.showCriteriaLevel3 = false;
        }

        //#region CriteriaType1 - for Reports only
        public setCriteriaTypeOneCheckOn = function (template: TemplateCriteriaDto) {
            var ctx = this;
            ctx.selectedCriteria.typeOneIsNotScreenableCriteriaItem.isOn = true;
        }

        public setCriteriaTypeOneCheckOff = function (template: TemplateCriteriaDto) {
            var ctx = this;
            ctx.selectedCriteria.typeOneIsNotScreenableCriteriaItem.isOn = false;
        }
        //#endregion

        //#region CriteriaType2
        public selectCriteriaType2 = function (templateCriteriaList: TemplateCriteriaDto[]) {
            var ctx = this;
            ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType2.tpl.html";
            ctx.selectedCriteria.loading = false;
            ctx.selectedCriteria.isUpdating = false;
            ctx.selectedCriteria.type = 2;
            ctx.selectedCriteria.typeTwoIsScreenableCriteriaItems = new Array();
            ctx.selectedCriteria.typeTwoIsNotScreenableCriteriaItems = new Array();

            for (var i = 0; i < templateCriteriaList.length; i++) {
                if (templateCriteriaList[i].isScreenable && !ctx.isReport) {
                    templateCriteriaList[i].score = 0;
                    ctx.selectedCriteria.typeTwoIsScreenableCriteriaItems.push(templateCriteriaList[i]);
                }
                else {
                    templateCriteriaList[i].isOn = false;
                    templateCriteriaList[i].isDataPoint = true;
                    ctx.selectedCriteria.typeTwoIsNotScreenableCriteriaItems.push(templateCriteriaList[i]);
                }
            }
        }

        public setCriteriaTypeTwoCheckOn = function (template: TemplateCriteriaDto) {
            var ctx = this;
            for (var i = 0; i < ctx.selectedCriteria.typeTwoIsNotScreenableCriteriaItems.length; i++) {
                if (template.id == ctx.selectedCriteria.typeTwoIsNotScreenableCriteriaItems[i].id) {
                    ctx.selectedCriteria.typeTwoIsNotScreenableCriteriaItems[i].isOn = true;
                }
            }
        }

        public setCriteriaTypeTwoCheckOff = function (template: TemplateCriteriaDto) {
            var ctx = this;
            for (var i = 0; i < ctx.selectedCriteria.typeTwoIsNotScreenableCriteriaItems.length; i++) {
                if (template.id == ctx.selectedCriteria.typeTwoIsNotScreenableCriteriaItems[i].id) {
                    ctx.selectedCriteria.typeTwoIsNotScreenableCriteriaItems[i].isOn = false;
                }
            }
        }
        //#endregion

        //#region CriteriaType3
        public loadCriteriaTypeThreeCollectionItems = (loadMore) => {
            var ctx = this;
            var skip = 0;
            if (loadMore) {
                skip = ctx.currentCriteriaTypeThree.collectionItems.length;
            }
            ctx.templateCriteriaService.getCriteriaTypeThreeCollectionItems(ctx.selectedCriteria.id, ctx.currentCriteriaTypeThree.filter, loadMore, skip, 20)
                .success((criteriaTypeThree: CriteriaTypeThree) => {
                ctx.notificationFactory.success();
                if (loadMore) {
                    var newItems = ctx.currentCriteriaTypeThree.collectionItems;
                    newItems.push.apply(newItems, criteriaTypeThree.collectionItems);
                    ctx.currentCriteriaTypeThree.collectionItems = newItems;
                } else {
                    ctx.currentCriteriaTypeThree.collectionItems = criteriaTypeThree.collectionItems;
                }
                ctx.currentCriteriaTypeThree.collectionItemsTotalCount = criteriaTypeThree.collectionItemsTotalCount;
            }).error((data, status, headers, config, statusText) => {
                ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
            });
        };

        public addItemToSelectedItemsType3 = (item) => {
            var ctx = this;
            var index = ctx.selectedCriteria.typeThreeSelectedItems.indexOf(item);
            if (index == -1) {
                ctx.selectedCriteria.typeThreeSelectedItems.push(item);
            }
        };

        public deleteSelectedItemsType3 = (item) => {
            var ctx = this;
            var index = ctx.selectedCriteria.typeThreeSelectedItems.indexOf(item);
            if (index != -1) {
                ctx.selectedCriteria.typeThreeSelectedItems.splice(index, 1);
            }
        };
        
        public toggleCriteriaTypeThree = function (template: TemplateCriteriaDto, status: boolean) {
            var ctx = this;
            for (var i = 0; i < ctx.selectedCriteria.typeThreeIsNotScreenableCriteriaItems.length; i++) {
                if (template.id == ctx.selectedCriteria.typeThreeIsNotScreenableCriteriaItems[i].id) {
                    ctx.selectedCriteria.typeThreeIsNotScreenableCriteriaItems[i].isOn = status;
                }
            }
        }
                
        public selectCriteriaType3 = function (templateCriteriaList: TemplateCriteriaDto[]) {

            var ctx = this;
            ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType3.tpl.html";
            ctx.selectedCriteria.loading = false;
            ctx.selectedCriteria.isUpdating = false;
            ctx.selectedCriteria.type = 3;
            ctx.selectedCriteria.isScreenable = templateCriteriaList.some(item => (!ctx.isReport && item.isScreenable));
            ctx.selectedCriteria.id = null;
            ctx.selectedCriteria.typeThreeIsScreenableCriteriaItems = new Array();
            ctx.selectedCriteria.typeThreeIsNotScreenableCriteriaItems = new Array();

            for (var i = 0; i < templateCriteriaList.length; i++) {
                if (templateCriteriaList[i].isScreenable && !ctx.isReport) {

                    ctx.activateBaseCriteria(templateCriteriaList[i].name);

                    ctx.selectedCriteria.id = templateCriteriaList[i].id;

                    ctx.templateCriteriaService.loadCriteriaTypeThree(templateCriteriaList[i].id, 0, 20).success((criteriaTypeThree: CriteriaTypeThree) => {
                        ctx.currentCriteriaTypeThree = criteriaTypeThree;
                        ctx.currentCriteriaTypeThree.filter = "";
                        ctx.selectedCriteria.typeThreeSelectedItems = criteriaTypeThree.selectedItems != null ? criteriaTypeThree.selectedItems : [];
                    }).error((data, status, headers, config, statusText) => {
                        ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                    });                    

                    ctx.selectedCriteria.typeThreeIsScreenableCriteriaItems.push(templateCriteriaList[i]);
                }
                else {
                    templateCriteriaList[i].isOn = false;
                    templateCriteriaList[i].isDataPoint = true;
                    ctx.selectedCriteria.typeThreeIsNotScreenableCriteriaItems.push(templateCriteriaList[i]);
                }
            }
        }
        //#endregion

        //#region CriteriaType4
        public selectCriteriaType4 = function (templateCriteriaList: TemplateCriteriaDto[]) {
            var ctx = this;
            ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType4.tpl.html";
            var piSymbol = templateCriteriaList[0].criteriaEntityField.split(" ")[0];
            ctx.selectedCriteria.type = 4;

            ctx.selectedCriteria.typeFourIsNotScreenableCriteriaItems = new Array();

            //add IsScreenable
            ctx.templateCriteriaService.loadCriteriaTypeFourCategories(piSymbol)
                .success((criteriaTypeFourCategories: any) => {
                ctx.selectedCriteria.type = 4;
                ctx.selectedCriteria.typeFourCategories = criteriaTypeFourCategories;
                ctx.selectedCriteria.typeFourSymbol = piSymbol;
                ctx.selectedCriteria.typeFourScreenOnAnyInvolvement = false;
            })
                .error(data => {
                ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(data));
            });

            //add DataPoints for CriteriaType4
            for (var i = 0; i < templateCriteriaList.length; i++) {
                if (!templateCriteriaList[i].isScreenable || ctx.isReport) {
                    templateCriteriaList[i].isOn = false;
                    templateCriteriaList[i].isDataPoint = true;
                    ctx.selectedCriteria.typeFourIsNotScreenableCriteriaItems.push(templateCriteriaList[i]);
                }
            }
        }

        public setCriteriaTypeFourCheckOn = function (template: TemplateCriteriaDto) {
            var ctx = this;
            for (var i = 0; i < ctx.selectedCriteria.typeFourIsNotScreenableCriteriaItems.length; i++) {
                if (template.id == ctx.selectedCriteria.typeFourIsNotScreenableCriteriaItems[i].id) {
                    ctx.selectedCriteria.typeFourIsNotScreenableCriteriaItems[i].isOn = true;
                }
            }
        }

        public setCriteriaTypeFourCheckOff = function (template: TemplateCriteriaDto) {
            var ctx = this;
            for (var i = 0; i < ctx.selectedCriteria.typeFourIsNotScreenableCriteriaItems.length; i++) {
                if (template.id == ctx.selectedCriteria.typeFourIsNotScreenableCriteriaItems[i].id) {
                    ctx.selectedCriteria.typeFourIsNotScreenableCriteriaItems[i].isOn = false;
                }
            }
        }

        public screenOnAnyInvolvementChecked = () => {
            if (this.selectedCriteria.typeFourScreenOnAnyInvolvement) {
                this.selectedCriteria.typeFourCategories.forEach((category) => {
                    if (category.significantOwnershipYesNo)
                        category.isSignificantOwnership = true;
                    if (!category.revenueRangeYesNo && !category.significantOwnershipYesNo)
                        category.isInvolved = true;
                    if (category.revenueRangeYesNo)
                        category.selectedValue = category.maxValue;
                });
            }
            else {
                this.selectedCriteria.typeFourCategories.forEach((category) => {
                    if (category.significantOwnershipYesNo)
                        category.isSignificantOwnership = false;
                    if (!category.revenueRangeYesNo && !category.significantOwnershipYesNo)
                        category.isInvolved = false;
                    if (category.revenueRangeYesNo)
                        category.selectedValue = 0;
                });
            }
        }

        public screenOnAnyInvolvementUncheck = () => {
            this.selectedCriteria.typeFourScreenOnAnyInvolvement = false;
        }

        public typeFourHasAnyInvolvement = (selectedCriteria: SelectedCriteriaDto): boolean => {
            var returnValue = false;
            if (selectedCriteria.typeFourCategories == undefined)
                return returnValue;
            selectedCriteria.typeFourCategories.forEach((category) => {
                if (category.revenueRange.length == 0) {
                    if (category.isInvolved || category.isSignificantOwnership)
                        returnValue = true;
                }

                if (category.revenueRange.length > 0) {
                    if ((category.selectedValue != undefined && category.selectedValue > 0) || category.isSignificantOwnership)
                        returnValue = true;
                }
            });

            return returnValue;
        };
        //#endregion

        //#region CriteriaType5
        public selectCriteriaType5 = function (templateCriteriaList: TemplateCriteriaDto[]) {
            var ctx = this;
            ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType5.tpl.html";
            ctx.selectedCriteria.loading = false;
            ctx.selectedCriteria.isUpdating = false;
            ctx.selectedCriteria.type = 5;
            ctx.selectedCriteria.typeFiveIsScreenableCriteriaItems = new Array();
            ctx.selectedCriteria.typeFiveIsNotScreenableCriteriaItems = new Array();

            for (var i = 0; i < templateCriteriaList.length; i++) {
                if (templateCriteriaList[i].isScreenable && !ctx.isReport) {
                    templateCriteriaList[i].score = -1;
                    templateCriteriaList[i].answersText = ctx.templateCriteriaService.composeAnswerText(templateCriteriaList[i].answerCategories, templateCriteriaList[i].score);
                    ctx.selectedCriteria.typeFiveIsScreenableCriteriaItems.push(templateCriteriaList[i]);
                }
                else {
                    templateCriteriaList[i].isOn = false;
                    templateCriteriaList[i].isDataPoint = true;
                    ctx.selectedCriteria.typeFiveIsNotScreenableCriteriaItems.push(templateCriteriaList[i]);
                }
            }
        }

        public setCriteriaTypeFiveCheckOn = function (template: TemplateCriteriaDto) {
            var ctx = this;
            for (var i = 0; i < ctx.selectedCriteria.typeFiveIsNotScreenableCriteriaItems.length; i++) {
                if (template.id == ctx.selectedCriteria.typeFiveIsNotScreenableCriteriaItems[i].id) {
                    ctx.selectedCriteria.typeFiveIsNotScreenableCriteriaItems[i].isOn = true;
                }
            }
        }

        public setCriteriaTypeFiveCheckOff = function (template: TemplateCriteriaDto) {
            var ctx = this;
            for (var i = 0; i < ctx.selectedCriteria.typeFiveIsNotScreenableCriteriaItems.length; i++) {
                if (template.id == ctx.selectedCriteria.typeFiveIsNotScreenableCriteriaItems[i].id) {
                    ctx.selectedCriteria.typeFiveIsNotScreenableCriteriaItems[i].isOn = false;
                }
            }
        }
        //#endregion

        //#region CriteriaTypeSix
        public selectCriteriaTypeSix = function (templateCriteriaList: TemplateCriteriaDto[]) {
            var ctx = this;
            ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType6.tpl.html";
            ctx.selectedCriteria.loading = false;
            ctx.selectedCriteria.isUpdating = false;
            ctx.criteriaTypeSixChart = null;
            ctx.selectedCriteria.id = null;
            ctx.selectedCriteria.type = 6;
            ctx.selectedCriteria.isScreenable = templateCriteriaList.some(item => item.isScreenable);
            ctx.selectedCriteria.typeSixIsScreenableCriteriaItems = new Array();
            ctx.selectedCriteria.typeSixIsNotScreenableCriteriaItems = new Array();

            for (var i = 0; i < templateCriteriaList.length; i++) {
                if (templateCriteriaList[i].isScreenable && !ctx.isReport) {
                    ctx.selectTemplateCriteria(templateCriteriaList[i]);
                }
                else {
                    templateCriteriaList[i].isOn = false;
                    templateCriteriaList[i].isDataPoint = true;
                    ctx.selectedCriteria.typeSixIsNotScreenableCriteriaItems.push(templateCriteriaList[i]);
                }
            }
        }

        public setCriteriaTypeSixCheckOn = function (template: TemplateCriteriaDto) {
            var ctx = this;
            for (var i = 0; i < ctx.selectedCriteria.typeSixIsNotScreenableCriteriaItems.length; i++) {
                if (template.id == ctx.selectedCriteria.typeSixIsNotScreenableCriteriaItems[i].id) {
                    ctx.selectedCriteria.typeSixIsNotScreenableCriteriaItems[i].isOn = true;
                }
            }
        }

        public setCriteriaTypeSixCheckOff = function (template: TemplateCriteriaDto) {
            var ctx = this;
            for (var i = 0; i < ctx.selectedCriteria.typeSixIsNotScreenableCriteriaItems.length; i++) {
                if (template.id == ctx.selectedCriteria.typeSixIsNotScreenableCriteriaItems[i].id) {
                    ctx.selectedCriteria.typeSixIsNotScreenableCriteriaItems[i].isOn = false;
                }
            }
        }

        //#endregion

        //#region CriteriaType7

        public selectCriteriaType7 = function (templateCriteriaList: TemplateCriteriaDto[]) {
            var ctx = this;
            ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType7.tpl.html";
            ctx.selectedCriteria.loading = false;
            ctx.selectedCriteria.isUpdating = false;
            ctx.selectedCriteria.type = 7;
            ctx.selectedCriteria.typeSevenIsScreenableCriteriaItems = new Array();
            ctx.selectedCriteria.typeSevenIsNotScreenableCriteriaItems = new Array();

            for (var i = 0; i < templateCriteriaList.length; i++) {
                if (templateCriteriaList[i].isScreenable && !ctx.isReport) {
                    if (templateCriteriaList[i].isScreenable)
                        templateCriteriaList[i].isSelected = false;
                    ctx.selectedCriteria.typeSevenIsScreenableCriteriaItems.push(templateCriteriaList[i]);
                }
                else {
                    templateCriteriaList[i].isOn = false;
                    templateCriteriaList[i].isDataPoint = true;
                    ctx.selectedCriteria.typeSevenIsNotScreenableCriteriaItems.push(templateCriteriaList[i]);
                }
            }
        }

        public addItemToSelectedItemsType7 = (item) => {
            var ctx = this;

            var index = ctx.selectedCriteria.typeSevenSelectedItems.indexOf(item);
            if (index == -1) {
                ctx.selectedCriteria.typeSevenSelectedItems.push(item);
            }
            ctx.buildGcSelectedItemsText();
        };

        public deleteSelectedItemsType7 = (item) => {
            var ctx = this;
            var index = ctx.selectedCriteria.typeSevenSelectedItems.indexOf(item);
            if (index != -1) {
                ctx.selectedCriteria.typeSevenSelectedItems.splice(index, 1);
            }
            ctx.buildGcSelectedItemsText();
        };

        public setCriteriaTypeSevenCheckOn = function (template: TemplateCriteriaDto) {
            var ctx = this;
            for (var i = 0; i < ctx.selectedCriteria.typeSevenIsNotScreenableCriteriaItems.length; i++) {
                if (template.id == ctx.selectedCriteria.typeSevenIsNotScreenableCriteriaItems[i].id) {
                    ctx.selectedCriteria.typeSevenIsNotScreenableCriteriaItems[i].isOn = true;
                }
            }
        }

        public setCriteriaTypeSevenCheckOff = function (template: TemplateCriteriaDto) {
            var ctx = this;
            for (var i = 0; i < ctx.selectedCriteria.typeSevenIsNotScreenableCriteriaItems.length; i++) {
                if (template.id == ctx.selectedCriteria.typeSevenIsNotScreenableCriteriaItems[i].id) {
                    ctx.selectedCriteria.typeSevenIsNotScreenableCriteriaItems[i].isOn = false;
                }
            }
        }

        public buildGcSelectedItemsText = () => {
            var ctx = this;
            ctx.gcSelectedItems = '';
            for (var i = 0; i < ctx.selectedCriteria.typeSevenSelectedItems.length; i++) {
                if (ctx.selectedCriteria.typeSevenSelectedItems[i] === "0")
                    ctx.gcSelectedItems += "Compliant ";

                if (ctx.selectedCriteria.typeSevenSelectedItems[i] === "1")
                    ctx.gcSelectedItems += "Watchlist ";

                if (ctx.selectedCriteria.typeSevenSelectedItems[i] === "2")
                    ctx.gcSelectedItems += "Non-Compliant ";
                
            }
            ctx.gcSelectedItems = ctx.gcSelectedItems.trim();
            ctx.gcSelectedItems = ctx.gcSelectedItems.replace(/ /g, ' and ');
            ctx.gcSelectedItems += ' companies are restricted.';
        };
        //#endregion

        //#region CriteriaType8
        public selectCriteriaType8 = function (templateCriteriaList: TemplateCriteriaDto[]) {
            var ctx = this;
            ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType8.tpl.html";
            ctx.selectedCriteria.loading = false;
            ctx.selectedCriteria.isUpdating = false;
            ctx.selectedCriteria.type = 8;
            ctx.selectedCriteria.typeEightIsScreenableCriteriaItems = new Array();
            ctx.selectedCriteria.typeEightIsNotScreenableCriteriaItems = new Array();

            for (var i = 0; i < templateCriteriaList.length; i++) {
                if (templateCriteriaList[i].isScreenable && !ctx.isReport) {
                    templateCriteriaList[i].score = 0;
                    ctx.selectedCriteria.typeEightIsScreenableCriteriaItems.push(templateCriteriaList[i]);
                }
                else {
                    templateCriteriaList[i].isOn = false;
                    templateCriteriaList[i].isDataPoint = true;
                    ctx.selectedCriteria.typeEightIsNotScreenableCriteriaItems.push(templateCriteriaList[i]);
                }
            }
        }

        public setCriteriaTypeEightCheckOn = function (template: TemplateCriteriaDto) {
            var ctx = this;
            for (var i = 0; i < ctx.selectedCriteria.typeEightIsNotScreenableCriteriaItems.length; i++) {
                if (template.id == ctx.selectedCriteria.typeEightIsNotScreenableCriteriaItems[i].id) {
                    ctx.selectedCriteria.typeEightIsNotScreenableCriteriaItems[i].isOn = true;
                }
            }
        }

        public setCriteriaTypeEightCheckOff = function (template: TemplateCriteriaDto) {
            var ctx = this;
            for (var i = 0; i < ctx.selectedCriteria.typeEightIsNotScreenableCriteriaItems.length; i++) {
                if (template.id == ctx.selectedCriteria.typeEightIsNotScreenableCriteriaItems[i].id) {
                    ctx.selectedCriteria.typeEightIsNotScreenableCriteriaItems[i].isOn = false;
                }
            }
        }
        //#endregion

        //#region CriteriaType9

        public selectCriteriaType9 = function (templateCriteriaList: TemplateCriteriaDto[]) {
            var ctx = this;
            ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType9.tpl.html";
            ctx.selectedCriteria.loading = false;
            ctx.selectedCriteria.isUpdating = false;
            ctx.selectedCriteria.type = 9;
            ctx.selectedCriteria.typeNineIsScreenableCriteriaItems = new Array();
            ctx.selectedCriteria.typeNineIsNotScreenableCriteriaItems = new Array();

            for (var i = 0; i < templateCriteriaList.length; i++) {
                if (templateCriteriaList[i].isScreenable && !ctx.isReport) {
                    templateCriteriaList[i].score = 0;
                    ctx.selectedCriteria.typeNineIsScreenableCriteriaItems.push(templateCriteriaList[i]);
                }
                else {
                    templateCriteriaList[i].isOn = false;
                    templateCriteriaList[i].isDataPoint = true;
                    ctx.selectedCriteria.typeNineIsNotScreenableCriteriaItems.push(templateCriteriaList[i]);
                }
            }
        }

        public setCriteriaTypeNineCheckOn = function (template: TemplateCriteriaDto) {
            var ctx = this;
            for (var i = 0; i < ctx.selectedCriteria.typeNineIsNotScreenableCriteriaItems.length; i++) {
                if (template.id == ctx.selectedCriteria.typeNineIsNotScreenableCriteriaItems[i].id) {
                    ctx.selectedCriteria.typeNineIsNotScreenableCriteriaItems[i].isOn = true;
                }
            }
        }

        public setCriteriaTypeNineCheckOff = function (template: TemplateCriteriaDto) {
            var ctx = this;
            for (var i = 0; i < ctx.selectedCriteria.typeNineIsNotScreenableCriteriaItems.length; i++) {
                if (template.id == ctx.selectedCriteria.typeNineIsNotScreenableCriteriaItems[i].id) {
                    ctx.selectedCriteria.typeNineIsNotScreenableCriteriaItems[i].isOn = false;
                }
            }
        }

        public getSelectionTextCriteriaType9 = (score: number) => {
            if (score === 0) {
                return "";
            }
            if (score === 10) {
                return "Category 10 involvement is restricted";
            }
            var restrictedCategories = [];
            for (var i = 10; i > score; i--) {
                restrictedCategories.push(i);
            }
            return "Categories " + restrictedCategories.join(", ") + " and " + score + " involvement is restricted";
        }

        //#endregion

        //#region CriteriaType10
        public selectCriteriaType10 = function (templateCriteriaList: TemplateCriteriaDto[]) {
            var ctx = this;
            ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType10.tpl.html";
            ctx.selectedCriteria.loading = false;
            ctx.selectedCriteria.isUpdating = false;
            ctx.selectedCriteria.type = 10;
            ctx.selectedCriteria.typeTenIsScreenableCriteriaItems = new Array();
            ctx.selectedCriteria.typeTenIsNotScreenableCriteriaItems = new Array();

            for (var i = 0; i < templateCriteriaList.length; i++) {
                //if (templateCriteriaList[i].isScreenable && !ctx.isReport) {
                //    templateCriteriaList[i].score = 0;
                //    ctx.selectedCriteria.typeTenIsScreenableCriteriaItems.push(templateCriteriaList[i]);
                //}
                //else {
                templateCriteriaList[i].isOn = false;
                templateCriteriaList[i].isDataPoint = true;
                ctx.selectedCriteria.typeTenIsNotScreenableCriteriaItems.push(templateCriteriaList[i]);
                //}
            }
        }

        public setCriteriaTypeTenCheckOn = function (template: TemplateCriteriaDto) {
            var ctx = this;
            for (var i = 0; i < ctx.selectedCriteria.typeTenIsNotScreenableCriteriaItems.length; i++) {
                if (template.id == ctx.selectedCriteria.typeTenIsNotScreenableCriteriaItems[i].id) {
                    ctx.selectedCriteria.typeTenIsNotScreenableCriteriaItems[i].isOn = true;
                }
            }
        }

        public setCriteriaTypeTenCheckOff = function (template: TemplateCriteriaDto) {
            var ctx = this;
            for (var i = 0; i < ctx.selectedCriteria.typeTenIsNotScreenableCriteriaItems.length; i++) {
                if (template.id == ctx.selectedCriteria.typeTenIsNotScreenableCriteriaItems[i].id) {
                    ctx.selectedCriteria.typeTenIsNotScreenableCriteriaItems[i].isOn = false;
                }
            }
        }

        //#endregion

        //#region CriteriaType16

        public selectCriteriaType16 = function (templateCriteriaList: TemplateCriteriaDto[]) {
            var ctx = this;
            ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType16.tpl.html";
            ctx.selectedCriteria.loading = false;
            ctx.selectedCriteria.isUpdating = false;
            ctx.selectedCriteria.type = 16;
            ctx.selectedCriteria.isScreenable = templateCriteriaList.some(item => item.isScreenable);
            ctx.selectedCriteria.type16IsScreenableCriteriaItems = new Array();
            ctx.selectedCriteria.type16IsNotScreenableCriteriaItems = new Array();

            for (var i = 0; i < templateCriteriaList.length; i++) {
                if (templateCriteriaList[i].isScreenable && !ctx.isReport) {
                    ctx.selectTemplateCriteria(templateCriteriaList[i]);
                }
                else {
                    templateCriteriaList[i].isOn = false;
                    templateCriteriaList[i].isDataPoint = true;
                    ctx.selectedCriteria.type16IsNotScreenableCriteriaItems.push(templateCriteriaList[i]);
                }
            }
        }

        public setCriteriaType16CheckOn = function (template: TemplateCriteriaDto) {
            var ctx = this;
            for (var i = 0; i < ctx.selectedCriteria.type16IsNotScreenableCriteriaItems.length; i++) {
                if (template.id == ctx.selectedCriteria.type16IsNotScreenableCriteriaItems[i].id) {
                    ctx.selectedCriteria.type16IsNotScreenableCriteriaItems[i].isOn = true;
                }
            }
        }

        public setCriteriaType16CheckOff = function (template: TemplateCriteriaDto) {
            var ctx = this;
            for (var i = 0; i < ctx.selectedCriteria.type16IsNotScreenableCriteriaItems.length; i++) {
                if (template.id == ctx.selectedCriteria.type16IsNotScreenableCriteriaItems[i].id) {
                    ctx.selectedCriteria.type16IsNotScreenableCriteriaItems[i].isOn = false;
                }
            }
        }

        //#endregion

        //#region CriteriaType17

        public selectCriteriaType17 = function (templateCriteriaList: TemplateCriteriaDto[]) {
            var ctx = this;
            ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType17.tpl.html";
            ctx.selectedCriteria.loading = false;
            ctx.selectedCriteria.isUpdating = false;
            ctx.selectedCriteria.type = 17;
            ctx.selectedCriteria.type17IsScreenableCriteriaItems = new Array();
            ctx.selectedCriteria.type17IsNotScreenableCriteriaItems = new Array();

            for (var i = 0; i < templateCriteriaList.length; i++) {
                if (templateCriteriaList[i].isScreenable && !ctx.isReport) {
                    templateCriteriaList[i].score = -1;
                    ctx.selectedCriteria.type17IsScreenableCriteriaItems.push(templateCriteriaList[i]);
                }
                else {
                    templateCriteriaList[i].isOn = false;
                    templateCriteriaList[i].isDataPoint = true;
                    ctx.selectedCriteria.type17IsNotScreenableCriteriaItems.push(templateCriteriaList[i]);
                }
            }
        }

        public setCriteriaType17CheckOn = function (template: TemplateCriteriaDto) {
            var ctx = this;
            for (var i = 0; i < ctx.selectedCriteria.type17IsNotScreenableCriteriaItems.length; i++) {
                if (template.id == ctx.selectedCriteria.type17IsNotScreenableCriteriaItems[i].id) {
                    ctx.selectedCriteria.type17IsNotScreenableCriteriaItems[i].isOn = true;
                }
            }
        }

        public setCriteriaType17CheckOff = function (template: TemplateCriteriaDto) {
            var ctx = this;
            for (var i = 0; i < ctx.selectedCriteria.type17IsNotScreenableCriteriaItems.length; i++) {
                if (template.id == ctx.selectedCriteria.type17IsNotScreenableCriteriaItems[i].id) {
                    ctx.selectedCriteria.type17IsNotScreenableCriteriaItems[i].isOn = false;
                }
            }
        }
        //#endregion

        //#region CriteriaType25
        public selectCriteriaType25 = function (templateCriteriaList: TemplateCriteriaDto[]) {
            var ctx = this;
            ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType25.tpl.html";
            ctx.selectedCriteria.loading = false;
            ctx.selectedCriteria.isUpdating = false;
            ctx.selectedCriteria.type = 25;
            ctx.selectedCriteria.type25IsScreenableCriteriaItems = new Array();
            ctx.selectedCriteria.type25IsNotScreenableCriteriaItems = new Array();

            for (var i = 0; i < templateCriteriaList.length; i++) {
                if (templateCriteriaList[i].isScreenable && !ctx.isReport) {
                    templateCriteriaList[i].score = -1;
                    templateCriteriaList[i].answersText = ctx.templateCriteriaService.composeAnswerText(templateCriteriaList[i].answerCategories, templateCriteriaList[i].score);
                    ctx.selectedCriteria.type25IsScreenableCriteriaItems.push(templateCriteriaList[i]);
                }
                else {
                    templateCriteriaList[i].isOn = false;
                    templateCriteriaList[i].isDataPoint = true;
                    ctx.selectedCriteria.type25IsNotScreenableCriteriaItems.push(templateCriteriaList[i]);
                }
            }
        }

        public setCriteriaType25CheckOn = function (template: TemplateCriteriaDto) {
            var ctx = this;
            for (var i = 0; i < ctx.selectedCriteria.type25IsNotScreenableCriteriaItems.length; i++) {
                if (template.id == ctx.selectedCriteria.type25IsNotScreenableCriteriaItems[i].id) {
                    ctx.selectedCriteria.type25IsNotScreenableCriteriaItems[i].isOn = true;
                }
            }
        }

        public setCriteriaType25CheckOff = function (template: TemplateCriteriaDto) {
            var ctx = this;
            for (var i = 0; i < ctx.selectedCriteria.type25IsNotScreenableCriteriaItems.length; i++) {
                if (template.id == ctx.selectedCriteria.type25IsNotScreenableCriteriaItems[i].id) {
                    ctx.selectedCriteria.type25IsNotScreenableCriteriaItems[i].isOn = false;
                }
            }
        }
        //#endregion

        //#region selectTemplateCriteria
         public selectTemplateCriteria = function (templateCriteria: TemplateCriteriaDto) {
            var ctx = this;
            ctx.selectedCriteria.loading = false;
            ctx.selectedCriteria.isUpdating = false;
            ctx.selectedCriteria.id = templateCriteria.id;
            ctx.selectedCriteria.name = templateCriteria.name;
            ctx.selectedCriteria.type = templateCriteria.criteriaType;
            ctx.activateBaseCriteria(templateCriteria.name);
            switch (templateCriteria.criteriaType) {
                case 1:
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType1.tpl.html";
                    if (ctx.isReport) {
                        var dataPoint = {
                            id: templateCriteria.id,
                            name: templateCriteria.name,
                            isDataPoint: true,
                            isOn: false
                        }
                        ctx.selectedCriteria.typeOneIsNotScreenableItem = dataPoint;
                    }
                    else {
                        ctx.templateCriteriaService.loadCriteriaTypeOne(templateCriteria.id).success((criteriaTypeOne: CriteriaTypeOne) => {
                            ctx.currentCriteriaTypeOne = criteriaTypeOne;
                            ctx.criteriaTypeOneChart = {};

                            var yAxis = [];
                            ctx.criteriaTypeOneChart.xAxis = [];
                            ctx.currentCriteriaTypeOne.criteriaData.forEach(function (item) {
                                ctx.criteriaTypeOneChart.xAxis.push(item.xAxisValue);
                                yAxis.push(item.yAxisValue);
                            });

                            var data = {
                                labels: ctx.criteriaTypeOneChart.xAxis.map(label => ''), // hide labels
                                datasets: [
                                    {
                                        label: "Companies",
                                        fillColor: "rgba(220,220,220,0.5)",
                                        strokeColor: "rgba(220,220,220,0.8)",
                                        highlightFill: "rgba(220,220,220,0.75)",
                                        highlightStroke: "rgba(220,220,220,1)",
                                        data: yAxis
                                    }
                                ]
                            };

                            var options = {
                                scaleBeginAtZero: true,
                                showTooltips: false,
                                scaleShowGridLines: false,
                                scaleGridLineColor: "rgba(0,0,0,0)",
                                barValueSpacing: 12
                            };

                            ctx.criteriaTypeOneChart.minValue = ctx.criteriaTypeOneChart.xAxis[0];
                            ctx.criteriaTypeOneChart.maxValue = ctx.criteriaTypeOneChart.xAxis[ctx.criteriaTypeOneChart.xAxis.length - 1];
                            ctx.criteriaTypeOneChart.data = data;
                            ctx.criteriaTypeOneChart.options = options;
                            ctx.criteriaTypeOneChart.invalidInput = ctx.chartInvalidInput(ctx.criteriaTypeOneChart);
                        }).error((data, status, headers, config, statusText) => {
                            ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                        });
                    }
                    break;
                case 3:
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType3.tpl.html";
                    if (!ctx.isReport && templateCriteria.isScreenable) {
                        ctx.templateCriteriaService.loadCriteriaTypeThree(templateCriteria.id, 0, 20).success((criteriaTypeThree: CriteriaTypeThree) => {
                            ctx.currentCriteriaTypeThree = criteriaTypeThree;
                            ctx.currentCriteriaTypeThree.filter = "";
                            ctx.selectedCriteria.typeThreeSelectedItems = criteriaTypeThree.selectedItems != null ? criteriaTypeThree.selectedItems : [];
                            //ctx.selectedCriteria.typeThreeCollectionName = criteriaTypeThree.collectionName;
                        }).error((data, status, headers, config, statusText) => {
                            ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                        });
                    }
                    break;
                case 6:
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType6.tpl.html";
                    if (!ctx.isReport && templateCriteria.isScreenable) {
                        ctx.templateCriteriaService.loadCriteriaTypeSix(templateCriteria.id).success((criteriaTypeSix: CriteriaTypeSix) => {
                            ctx.currentCriteriaTypeSix = criteriaTypeSix;
                            ctx.criteriaTypeSixChart = {};

                            var yAxis = [];
                            ctx.criteriaTypeSixChart.xAxis = [];
                            ctx.currentCriteriaTypeSix.criteriaData.forEach(function (item) {
                                ctx.criteriaTypeSixChart.xAxis.push(item.xAxisValue);
                                yAxis.push(item.yAxisValue);
                            });

                            var data = {
                                labels: ctx.criteriaTypeSixChart.xAxis.map(label => ''), // hide labels
                                datasets: [{
                                    label: "Companies",
                                    fillColor: "rgba(220,220,220,0.5)",
                                    strokeColor: "rgba(220,220,220,0.8)",
                                    highlightFill: "rgba(220,220,220,0.75)",
                                    highlightStroke: "rgba(220,220,220,1)",
                                    data: yAxis
                                }]
                            };

                            var options = {
                                scaleBeginAtZero: true,
                                showTooltips: false,
                                scaleShowGridLines: false,
                                scaleGridLineColor: "rgba(0,0,0,0)",
                                barValueSpacing: 7
                            };

                            ctx.criteriaTypeSixChart.minValue = ctx.criteriaTypeSixChart.xAxis[0];
                            ctx.criteriaTypeSixChart.maxValue = ctx.criteriaTypeSixChart.xAxis[ctx.criteriaTypeSixChart.xAxis.length - 1];
                            ctx.criteriaTypeSixChart.data = data;
                            ctx.criteriaTypeSixChart.options = options;
                            ctx.criteriaTypeSixChart.invalidInput = ctx.chartInvalidInput(ctx.criteriaTypeSixChart);
                        }).error((data, status, headers, config, statusText) => {
                            ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                        });
                    }
                    break;
                case 7:
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType7.tpl.html";
                    ctx.templateCriteriaService.loadCriteriaTypeSeven(templateCriteria.id, 0, 20).success((criteriaTypeSeven: CriteriaTypeSeven) => {
                        ctx.currentCriteriaTypeSeven = criteriaTypeSeven;
                        ctx.selectedCriteria.typeSevenSelectedItems = criteriaTypeSeven.selectedItems != null ? criteriaTypeSeven.selectedItems : [];
                        ctx.selectedCriteria.typeSevenCollectionName = criteriaTypeSeven.collectionName;
                    }).error((data, status, headers, config, statusText) => {
                        ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                    });
                    break;
                case 9:
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType9.tpl.html";
                    break;
                case 16:
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType16.tpl.html";
                    if (ctx.isReport) {
                        var dataPoint = {
                            id: templateCriteria.id,
                            name: templateCriteria.name,
                            isDataPoint: true,
                            isOn: false
                        }
                        ctx.selectedCriteria.type16IsNotScreenableItem = dataPoint;
                    }
                    else {
                        ctx.templateCriteriaService.loadCriteriaType16(templateCriteria.id).success((criteriaType16: CriteriaType16) => {
                            ctx.currentCriteriaType16 = criteriaType16;
                            ctx.criteriaType16Chart = {};

                            var yAxis = [];
                            ctx.criteriaType16Chart.xAxis = [];
                            ctx.currentCriteriaType16.criteriaData.forEach(function (item) {
                                ctx.criteriaType16Chart.xAxis.push(item.xAxisValue);
                                yAxis.push(item.yAxisValue);
                            });

                            var data = {
                                labels: ctx.criteriaType16Chart.xAxis.map(label => ''),
                                datasets: [{
                                    label: "Companies",
                                    fillColor: "rgba(220,220,220,0.5)",
                                    strokeColor: "rgba(220,220,220,0.8)",
                                    highlightFill: "rgba(220,220,220,0.75)",
                                    highlightStroke: "rgba(220,220,220,1)",
                                    data: yAxis
                                }]
                            };

                            var options = {
                                scaleBeginAtZero: true,
                                showTooltips: false,
                                scaleShowGridLines: false,
                                scaleGridLineColor: "rgba(0,0,0,0)",
                                barValueSpacing: 12
                            };

                            ctx.criteriaType16Chart.minValue = ctx.criteriaType16Chart.xAxis[0];
                            ctx.criteriaType16Chart.maxValue = ctx.criteriaType16Chart.xAxis[ctx.criteriaType16Chart.xAxis.length - 1];
                            ctx.criteriaType16Chart.data = data;
                            ctx.criteriaType16Chart.options = options;
                            ctx.criteriaType16Chart.invalidInput = ctx.chartInvalidInput(ctx.criteriaType16Chart);
                        }).error((data, status, headers, config, statusText) => {
                            ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                        });
                    }
                    break;
                case 17:
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType17.tpl.html";
                    break;
                case 25:
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType25.tpl.html";
                    break;
                case 26:
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType6.tpl.html";
                    break;
                default:
                    ctx.currentSelection = "";
                    break;
            }
        }
        //#endregion

        //#region selectCriteria
        public selectCriteria = function (baseCriteria: BaseCriteria) {
            var ctx = this;

            ctx.selectedCriteria.loading = false;
            ctx.selectedCriteria.isUpdating = false;
            ctx.selectedCriteria.id = baseCriteria.id;
            ctx.selectedCriteria.name = baseCriteria.name;
            ctx.selectedCriteria.type = baseCriteria.type;
            ctx.activateBaseCriteria(baseCriteria.name);
            switch (baseCriteria.type) {
                case 1:
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType1.tpl.html";
                    ctx.templateCriteriaService.loadCriteriaTypeOne(baseCriteria.id).success((criteriaTypeOne: CriteriaTypeOne) => {
                        ctx.currentCriteriaTypeOne = criteriaTypeOne;
                        ctx.criteriaTypeOneChart = {};

                        var yAxis = [];
                        ctx.criteriaTypeOneChart.xAxis = [];
                        ctx.currentCriteriaTypeOne.criteriaData.forEach(function (item) {
                            ctx.criteriaTypeOneChart.xAxis.push(item.xAxisValue);
                            yAxis.push(item.yAxisValue);
                        });

                        var data = {
                            labels: ctx.criteriaTypeOneChart.xAxis.map(label => ''),
                            datasets: [
                                {
                                    label: "Companies",
                                    fillColor: "rgba(220,220,220,0.5)",
                                    strokeColor: "rgba(220,220,220,0.8)",
                                    highlightFill: "rgba(220,220,220,0.75)",
                                    highlightStroke: "rgba(220,220,220,1)",
                                    data: yAxis
                                }
                            ]
                        };

                        var options = {
                            scaleBeginAtZero: true,
                            showTooltips: false,
                            scaleShowGridLines: false,
                            scaleGridLineColor: "rgba(0,0,0,0)",
                            barValueSpacing: 12
                        };

                        ctx.criteriaTypeOneChart.minValue = ctx.criteriaTypeOneChart.xAxis[0];
                        ctx.criteriaTypeOneChart.maxValue = ctx.criteriaTypeOneChart.xAxis[ctx.criteriaTypeOneChart.xAxis.length - 1];
                        ctx.criteriaTypeOneChart.data = data;
                        ctx.criteriaTypeOneChart.options = options;
                        ctx.criteriaTypeOneChart.invalidInput = ctx.chartInvalidInput(ctx.criteriaTypeOneChart);
                    }).error((data, status, headers, config, statusText) => {
                        ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                    });
                    break;
                case 2:
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType2.tpl.html";
                    ctx.selectedCriteria.score = 0;
                    break;
                case 3:
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType3.tpl.html";
                    ctx.templateCriteriaService.loadCriteriaTypeThree(baseCriteria.id, 0, 20).success((criteriaTypeThree: CriteriaTypeThree) => {
                        ctx.currentCriteriaTypeThree = criteriaTypeThree;
                        ctx.currentCriteriaTypeThree.filter = "";
                        ctx.selectedCriteria.typeThreeSelectedItems = criteriaTypeThree.selectedItems != null ? criteriaTypeThree.selectedItems : [];
                        ctx.selectedCriteria.typeThreeCollectionName = criteriaTypeThree.collectionName;
                    }).error((data, status, headers, config, statusText) => {
                        ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                    });
                    break;
                case 4:
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType4.tpl.html";

                    ctx.templateCriteriaService.loadCriteriaTypeFourCategories(baseCriteria.symbol).success((criteriaTypeFourCategories: CriteriaTypeFourCategories[]) => {
                        ctx.selectedCriteria.name = baseCriteria.name;
                        ctx.selectedCriteria.type = baseCriteria.type;
                        ctx.selectedCriteria.typeFourCategories = criteriaTypeFourCategories;
                        ctx.selectedCriteria.typeFourSymbol = baseCriteria.symbol;
                        ctx.selectedCriteria.typeFourScreenOnAnyInvolvement = false;
                    }).error(data => {
                        ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(data));
                    });

                    break;
                case 5://criteriafive
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType5.tpl.html";
                    ctx.templateCriteriaService.loadTemplateCriteriaTypeFive(baseCriteria.id).success((criteriaTypeFive: CriteriaTypeFive) => {
                        criteriaTypeFive.score = -1;
                        ctx.currentCriteriaTypeFive = criteriaTypeFive;
                    }).error((data, status, headers, config, statusText) => {
                        ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                    });
                    break;
                case 6:
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType6.tpl.html";
                    ctx.templateCriteriaService.loadCriteriaTypeSix(baseCriteria.id).success((criteriaTypeSix: CriteriaTypeSix) => {
                        ctx.currentCriteriaTypeSix = criteriaTypeSix;
                        ctx.criteriaTypeSixChart = {};

                        var yAxis = [];
                        ctx.criteriaTypeSixChart.xAxis = [];
                        ctx.currentCriteriaTypeSix.criteriaData.forEach(function (item) {
                            ctx.criteriaTypeSixChart.xAxis.push(item.xAxisValue);
                            yAxis.push(item.yAxisValue);
                        });

                        var data = {
                            labels: ctx.criteriaTypeSixChart.xAxis.map(label => ''),
                            datasets: [{
                                label: "Companies",
                                fillColor: "rgba(220,220,220,0.5)",
                                strokeColor: "rgba(220,220,220,0.8)",
                                highlightFill: "rgba(220,220,220,0.75)",
                                highlightStroke: "rgba(220,220,220,1)",
                                data: yAxis
                            }]
                        };

                        var options = {
                            scaleBeginAtZero: true,
                            showTooltips: false,
                            scaleShowGridLines: false,
                            scaleGridLineColor: "rgba(0,0,0,0)",
                            barValueSpacing: 7
                        };

                        ctx.criteriaTypeSixChart.minValue = ctx.criteriaTypeSixChart.xAxis[0];
                        ctx.criteriaTypeSixChart.maxValue = ctx.criteriaTypeSixChart.xAxis[ctx.criteriaTypeSixChart.xAxis.length - 1];
                        ctx.criteriaTypeSixChart.data = data;
                        ctx.criteriaTypeSixChart.options = options;
                        ctx.criteriaTypeSixChart.invalidInput = ctx.chartInvalidInput(ctx.criteriaTypeSixChart);
                    }).error((data, status, headers, config, statusText) => {
                        ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                    });
                    break;
                case 7:
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType7.tpl.html";
                    ctx.templateCriteriaService.loadCriteriaTypeSeven(baseCriteria.id, 0, 20)
                        .success((criteriaTypeSeven: CriteriaTypeSeven) => {
                        ctx.currentCriteriaTypeSeven = criteriaTypeSeven;
                        ctx.selectedCriteria.typeSevenSelectedItems = criteriaTypeSeven.selectedItems != null ? criteriaTypeSeven.selectedItems : [];
                        ctx.selectedCriteria.typeSevenCollectionName = criteriaTypeSeven.collectionName;
                    })
                        .error((data, status, headers, config, statusText) => {
                        ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                    });
                    break;
                case 8:
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType8.tpl.html";
                    ctx.selectedCriteria.score = 0;
                    break;
                case 9:
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType9.tpl.html";
                    break;
                case 16:
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType16.tpl.html";
                    ctx.templateCriteriaService.loadCriteriaType16(baseCriteria.id).success((criteriaType16: CriteriaType16) => {
                        ctx.currentCriteriaType16 = criteriaType16;
                        ctx.criteriaType16Chart = {};

                        var yAxis = [];
                        ctx.criteriaType16Chart.xAxis = [];
                        ctx.currentCriteriaType16.criteriaData.forEach(function (item) {
                            ctx.criteriaType16Chart.xAxis.push(item.xAxisValue);
                            yAxis.push(item.yAxisValue);
                        });

                        var data = {
                            labels: ctx.criteriaType16Chart.xAxis.map(label => ''),
                            datasets: [{
                                label: "Companies",
                                fillColor: "rgba(220,220,220,0.5)",
                                strokeColor: "rgba(220,220,220,0.8)",
                                highlightFill: "rgba(220,220,220,0.75)",
                                highlightStroke: "rgba(220,220,220,1)",
                                data: yAxis
                            }]
                        };

                        var options = {
                            scaleBeginAtZero: true,
                            showTooltips: false,
                            scaleShowGridLines: false,
                            scaleGridLineColor: "rgba(0,0,0,0)",
                            barValueSpacing: 12
                        };

                        ctx.criteriaType16Chart.minValue = ctx.criteriaType16Chart.xAxis[0];
                        ctx.criteriaType16Chart.maxValue = ctx.criteriaType16Chart.xAxis[ctx.criteriaType16Chart.xAxis.length - 1];
                        ctx.criteriaType16Chart.data = data;
                        ctx.criteriaType16Chart.options = options;
                        ctx.criteriaType16Chart.invalidInput = ctx.chartInvalidInput(ctx.criteriaType16Chart);
                    }).error((data, status, headers, config, statusText) => {
                        ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                    });
                    break;
                case 17:
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType17.tpl.html";
                    break;
                case 25://criteriafive
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType25.tpl.html";
                    ctx.templateCriteriaService.loadTemplateCriteriaType25(baseCriteria.id).success((criteriaType25: CriteriaType25) => {
                        criteriaType25.score = -1;
                        ctx.currentCriteriaType25 = criteriaType25;
                    }).error((data, status, headers, config, statusText) => {
                        ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                    });
                    break;
                default:
                    ctx.currentSelection = "";
                    break;
            }
        };
        //#endregion

        //#region utils

        public pushUniqueItems(arrayItems, item, checkId) {
            var newArray = new Array();
            for (var i = 0; i < arrayItems.length; i++) {
                if (arrayItems[i][checkId] != item[checkId])
                    newArray.push(arrayItems[i]);
            }
            newArray.push(item);
            return newArray;
        }

        //used for pushing the value of an object to an array (not the reference)
        public clone(obj) {
            if (null == obj || "object" != typeof obj) return obj;
            var copy = obj.constructor();
            for (var attr in obj) {
                if (obj.hasOwnProperty(attr))
                    copy[attr] = obj[attr];
            }
            return copy;
        }

        public groupBy(array, f) {
            var groups = {};
            array.forEach(function (o) {
                var group = JSON.stringify(f(o));
                groups[group] = groups[group] || [];
                groups[group].push(o);
            });
            return Object.keys(groups).map(function (group) {
                return groups[group];
            });
        }

        //#endregion

        //#region AddCriteria
        public addCriteriaToSelectedItems(criteriaLevel2Id: string) {
            var ctx = this;
            switch (ctx.selectedCriteria.type) {
                case 1:
                    if (!ctx.isReport && ctx.selectedCriteria.isScreenable) {
                        var criteriaTypeOne = {
                            criteriaLevel3Id: ctx.currentCriteriaLevel3Id,
                            criteriaLevel2Id: criteriaLevel2Id,
                            id: ctx.selectedCriteria.id,
                            name: ctx.selectedCriteria.name,
                            type: ctx.selectedCriteria.type,
                            minValue: ctx.criteriaTypeOneChart.minValue,
                            maxValue: ctx.criteriaTypeOneChart.maxValue
                        };
                        ctx.listSelectedFilterCriteria = ctx.pushUniqueItems(ctx.listSelectedFilterCriteria, criteriaTypeOne, "criteriaLevel3Id");
                    }
                    else {
                        var criteriaTypeOneDataPoint = {
                            criteriaLevel3Id: ctx.currentCriteriaLevel3Id,
                            criteriaLevel2Id: criteriaLevel2Id,
                            id: ctx.selectedCriteria.typeOneIsNotScreenableCriteriaItem.id,
                            name: ctx.selectedCriteria.typeOneIsNotScreenableCriteriaItem.name,
                            isOn: ctx.selectedCriteria.typeOneIsNotScreenableCriteriaItem.isOn,
                            isDataPoint: true,
                            type: ctx.selectedCriteria.type,
                        };
                        ctx.listSelectedFilterCriteria = ctx.pushUniqueItems(ctx.listSelectedFilterCriteria, criteriaTypeOneDataPoint, "criteriaLevel3Id");
                    }
                    break;
                case 2:
                    var isScreenableItems = new Array();
                    for (var i = 0; i < ctx.selectedCriteria.typeTwoIsScreenableCriteriaItems.length; i++) {
                        if (ctx.selectedCriteria.typeTwoIsScreenableCriteriaItems[i].score != 0) {
                            isScreenableItems.push(ctx.selectedCriteria.typeTwoIsScreenableCriteriaItems[i]);
                        }
                    }
                    var isNotScreenableCriteriaItems = new Array();
                    for (var i = 0; i < ctx.selectedCriteria.typeTwoIsNotScreenableCriteriaItems.length; i++) {
                        var copy = ctx.clone(ctx.selectedCriteria.typeTwoIsNotScreenableCriteriaItems[i]);
                        isNotScreenableCriteriaItems.push(copy);
                    }
                    var criteriaTypeTwo = {
                        criteriaLevel3Id: ctx.currentCriteriaLevel3Id,
                        criteriaLevel2Id: criteriaLevel2Id,
                        type: ctx.selectedCriteria.type,
                        isScreenableCriteriaItems: isScreenableItems,
                        isNotScreenableCriteriaItems: isNotScreenableCriteriaItems
                    };
                    ctx.listSelectedFilterCriteria = ctx.pushUniqueItems(ctx.listSelectedFilterCriteria, criteriaTypeTwo, "criteriaLevel3Id");
                    break;
                case 3:
                    var criteriaTypeThree: any = {
                        criteriaLevel3Id: ctx.currentCriteriaLevel3Id,
                        criteriaLevel2Id: criteriaLevel2Id,
                        type: ctx.selectedCriteria.type,
                        filterGroupId: ctx.selectedCriteria.filterGroupId,                        
                    };

                    var isScreenableCriteriaItems = new Array();
                    if (!ctx.isReport && ctx.selectedCriteria.isScreenable) {
                        ctx.selectedCriteria.typeThreeIsScreenableCriteriaItems.forEach(screenableItem => {
                            if (ctx.selectedCriteria.typeThreeSelectedItems && ctx.selectedCriteria.typeThreeSelectedItems.length > 0) {
                                var criteriaTypeThreeIsScreanable = {
                                    criteriaLevel3Id: ctx.currentCriteriaLevel3Id,
                                    criteriaLevel2Id: criteriaLevel2Id,
                                    id: screenableItem.id,
                                    name: screenableItem.name,
                                    type: screenableItem.type,
                                    selectedItems: ctx.selectedCriteria.typeThreeSelectedItems.slice()
                                };
                                isScreenableCriteriaItems.push(criteriaTypeThreeIsScreanable);
                            }
                        });                        
                    }
                    criteriaTypeThree.isScreenableCriteriaItems = isScreenableCriteriaItems;
                    
                    var isNotScreenableCriteriaItems = new Array();
                    ctx.selectedCriteria.typeThreeIsNotScreenableCriteriaItems.forEach(notScreenableItem => {
                        if (notScreenableItem.isOn) {
                            var copy = ctx.clone(notScreenableItem);
                            isNotScreenableCriteriaItems.push(copy);
                        }
                    });
                    criteriaTypeThree.isNotScreenableCriteriaItems = isNotScreenableCriteriaItems;

                    ctx.listSelectedFilterCriteria = ctx.pushUniqueItems(ctx.listSelectedFilterCriteria, criteriaTypeThree, "criteriaLevel3Id");
                    break;
                case 4:
                    var selectedCategories = [];

                    ctx.selectedCriteria.typeFourCategories.forEach((category) => {
                        if (category.isInvolved || category.isSignificantOwnership || category.selectedValue > 0) {
                            if (category.selectedValue > 0) {
                                selectedCategories.push(
                                    {
                                        categoryOfInvolvement: category.categoryOfInvolvement,
                                        isSignificantOwnership: false,
                                        isInvolvement: false,
                                        revenueRange: category.selectedValue > 0,
                                        start: category.revenueRange[5 - category.selectedValue].start,
                                        stop: category.revenueRange[5 - category.selectedValue].stop,
                                        name: category.name,
                                        categorySymbol: category.categorySymbol,
                                        id: category.templateCriteriaId
                                    });
                            }

                            if (category.isInvolved) {
                                selectedCategories.push(
                                    {
                                        categoryOfInvolvement: category.categoryOfInvolvement,
                                        isSignificantOwnership: false,
                                        isInvolvement: category.isInvolved,
                                        revenueRange: false,
                                        start: 0,
                                        stop: 0,
                                        name: category.name,
                                        categorySymbol: category.categorySymbol,
                                        id: category.templateCriteriaId
                                    });
                            }

                            if (category.isSignificantOwnership) {
                                selectedCategories.push(
                                    {
                                        categoryOfInvolvement: category.categoryOfInvolvement,
                                        isSignificantOwnership: category.isSignificantOwnership,
                                        isInvolvement: false,
                                        revenueRange: false,
                                        start: 0,
                                        stop: 0,
                                        name: category.name,
                                        categorySymbol: category.categorySymbol,
                                        id: category.templateCriteriaId
                                    });
                            }
                        }
                    });
                    var isNotScreenableCriteriaItems = new Array();
                    for (var i = 0; i < ctx.selectedCriteria.typeFourIsNotScreenableCriteriaItems.length; i++) {
                        var copy = ctx.clone(ctx.selectedCriteria.typeFourIsNotScreenableCriteriaItems[i]);
                        isNotScreenableCriteriaItems.push(copy);
                    }
                    var criteriaTypeFour = {
                        criteriaLevel3Id: ctx.currentCriteriaLevel3Id,
                        criteriaLevel2Id: criteriaLevel2Id,
                        id: ctx.selectedCriteria.id,
                        name: ctx.selectedCriteria.name,
                        type: ctx.selectedCriteria.type,
                        filterGroupId: ctx.selectedCriteria.filterGroupId,
                        selectedCategories: selectedCategories.slice(),
                        isNotScreenableCriteriaItems: isNotScreenableCriteriaItems,
                        claimSymbol: ctx.selectedCriteria.typeFourSymbol
                    }

                    ctx.listSelectedFilterCriteria = ctx.pushUniqueItems(ctx.listSelectedFilterCriteria, criteriaTypeFour, "criteriaLevel3Id");
                    break;
                case 5:
                    var isScreenableItems = new Array();
                    for (var i = 0; i < ctx.selectedCriteria.typeFiveIsScreenableCriteriaItems.length; i++) {
                        if (ctx.scoreIsSelected(ctx.selectedCriteria.typeFiveIsScreenableCriteriaItems[i].score, 5)) {
                            var copy = ctx.clone(ctx.selectedCriteria.typeFiveIsScreenableCriteriaItems[i]);
                            isScreenableItems.push(copy);
                        }
                    }
                    var isNotScreenableCriteriaItems = new Array();
                    for (var i = 0; i < ctx.selectedCriteria.typeFiveIsNotScreenableCriteriaItems.length; i++) {
                        var copy = ctx.clone(ctx.selectedCriteria.typeFiveIsNotScreenableCriteriaItems[i]);
                        isNotScreenableCriteriaItems.push(copy);
                    }
                    var criteriaTypeFive = {
                        criteriaLevel3Id: ctx.currentCriteriaLevel3Id,
                        criteriaLevel2Id: criteriaLevel2Id,
                        type: ctx.selectedCriteria.type,
                        isScreenableCriteriaItems: isScreenableItems,
                        isNotScreenableCriteriaItems: isNotScreenableCriteriaItems
                    };
                    ctx.listSelectedFilterCriteria = ctx.pushUniqueItems(ctx.listSelectedFilterCriteria, criteriaTypeFive, "criteriaLevel3Id");
                    break;
                case 6:
                    var isNotScreenableCriteriaItems = new Array();
                    for (var i = 0; i < ctx.selectedCriteria.typeSixIsNotScreenableCriteriaItems.length; i++) {
                        var copy = ctx.clone(ctx.selectedCriteria.typeSixIsNotScreenableCriteriaItems[i]);
                        isNotScreenableCriteriaItems.push(copy);
                    }

                    var criteriaTypeSix: any = {
                        criteriaLevel3Id: ctx.currentCriteriaLevel3Id,
                        criteriaLevel2Id: criteriaLevel2Id,
                        id: ctx.selectedCriteria.id || ctx.selectedCriteria.typeSixIsNotScreenableCriteriaItems[0].id,
                        name: ctx.selectedCriteria.name,
                        type: ctx.selectedCriteria.type,
                        isNotScreenableCriteriaItems: isNotScreenableCriteriaItems
                    };
                    if (!ctx.isReport && ctx.selectedCriteria.isScreenable) {
                        criteriaTypeSix.minValue = ctx.criteriaTypeSixChart.minValue;
                        criteriaTypeSix.maxValue = ctx.criteriaTypeSixChart.maxValue;
                    } else {
                        criteriaTypeSix.removed = true;
                        criteriaTypeSix.isDataPoint = true;
                    }
                    ctx.listSelectedFilterCriteria = ctx.pushUniqueItems(ctx.listSelectedFilterCriteria, criteriaTypeSix, "criteriaLevel3Id");
                    break;
                case 7:
                    var isScreenableItems = new Array();
                    for (var i = 0; i < ctx.selectedCriteria.typeSevenIsScreenableCriteriaItems.length; i++) {
                        if (ctx.selectedCriteria.typeSevenSelectedItems.length > 0) {
                            var criteriaTypeSevenIsScreenable = {
                                criteriaLevel3Id: ctx.currentCriteriaLevel3Id,
                                criteriaLevel2Id: criteriaLevel2Id,
                                id: ctx.selectedCriteria.typeSevenIsScreenableCriteriaItems[0].id,
                                name: ctx.selectedCriteria.typeSevenIsScreenableCriteriaItems[0].name,
                                type: ctx.selectedCriteria.typeSevenIsScreenableCriteriaItems[0].type,
                                selectedItems: ctx.selectedCriteria.typeSevenSelectedItems,
                            };
                            isScreenableItems.push(criteriaTypeSevenIsScreenable);
                        }
                    }
                    var isNotScreenableCriteriaItems = new Array();
                    for (var i = 0; i < ctx.selectedCriteria.typeSevenIsNotScreenableCriteriaItems.length; i++) {
                        var copy = ctx.clone(ctx.selectedCriteria.typeSevenIsNotScreenableCriteriaItems[i]);
                        isNotScreenableCriteriaItems.push(copy);
                    }
                    var criteriaTypeSeven = {
                        criteriaLevel3Id: ctx.currentCriteriaLevel3Id,
                        criteriaLevel2Id: criteriaLevel2Id,
                        type: ctx.selectedCriteria.type,
                        isScreenableCriteriaItems: isScreenableItems.slice(),
                        isNotScreenableCriteriaItems: isNotScreenableCriteriaItems.slice()
                    };
                    ctx.listSelectedFilterCriteria = ctx.pushUniqueItems(ctx.listSelectedFilterCriteria, criteriaTypeSeven, "criteriaLevel3Id");
                    break;
                case 8:
                    var isScreenableItems = new Array();
                    for (var i = 0; i < ctx.selectedCriteria.typeEightIsScreenableCriteriaItems.length; i++) {
                        if (ctx.scoreIsSelected(ctx.selectedCriteria.typeEightIsScreenableCriteriaItems[i].score, 8))
                            isScreenableItems.push(ctx.selectedCriteria.typeEightIsScreenableCriteriaItems[i]);
                    }

                    var isNotScreenableCriteriaItems = new Array();
                    for (var i = 0; i < ctx.selectedCriteria.typeEightIsNotScreenableCriteriaItems.length; i++) {
                        var copy = ctx.clone(ctx.selectedCriteria.typeEightIsNotScreenableCriteriaItems[i]);
                        isNotScreenableCriteriaItems.push(copy);
                    }

                    var criteriaTypeEight = {
                        criteriaLevel3Id: ctx.currentCriteriaLevel3Id,
                        criteriaLevel2Id: criteriaLevel2Id,
                        type: ctx.selectedCriteria.type,
                        isScreenableCriteriaItems: isScreenableItems,
                        isNotScreenableCriteriaItems: isNotScreenableCriteriaItems
                    };
                    ctx.listSelectedFilterCriteria = ctx.pushUniqueItems(ctx.listSelectedFilterCriteria, criteriaTypeEight, "criteriaLevel3Id");
                    break;
                case 9:
                    var isScreenableItems = new Array();
                    for (var i = 0; i < ctx.selectedCriteria.typeNineIsScreenableCriteriaItems.length; i++) {
                        if (ctx.scoreIsSelected(ctx.selectedCriteria.typeNineIsScreenableCriteriaItems[i].score, 9))
                            isScreenableItems.push(ctx.selectedCriteria.typeNineIsScreenableCriteriaItems[i]);
                    }

                    var isNotScreenableCriteriaItems = new Array();
                    for (var i = 0; i < ctx.selectedCriteria.typeNineIsNotScreenableCriteriaItems.length; i++) {
                        var copy = ctx.clone(ctx.selectedCriteria.typeNineIsNotScreenableCriteriaItems[i]);
                        isNotScreenableCriteriaItems.push(copy);
                    }

                    var criteriaTypeNine = {
                        criteriaLevel3Id: ctx.currentCriteriaLevel3Id,
                        criteriaLevel2Id: criteriaLevel2Id,
                        type: ctx.selectedCriteria.type,
                        isScreenableCriteriaItems: isScreenableItems,
                        isNotScreenableCriteriaItems: isNotScreenableCriteriaItems
                    };
                    ctx.listSelectedFilterCriteria = ctx.pushUniqueItems(ctx.listSelectedFilterCriteria, criteriaTypeNine, "criteriaLevel3Id");
                    break;
                case 10:
                    var isScreenableItems = new Array();
                    for (var i = 0; i < ctx.selectedCriteria.typeTenIsScreenableCriteriaItems.length; i++) {
                        if (ctx.selectedCriteria.typeTenIsScreenableCriteriaItems[i].score >= 1)
                            isScreenableItems.push(ctx.selectedCriteria.typeTenIsScreenableCriteriaItems[i]);
                    }

                    var isNotScreenableCriteriaItems = new Array();
                    for (var i = 0; i < ctx.selectedCriteria.typeTenIsNotScreenableCriteriaItems.length; i++) {
                        var copy = ctx.clone(ctx.selectedCriteria.typeTenIsNotScreenableCriteriaItems[i]);
                        isNotScreenableCriteriaItems.push(copy);
                    }

                    var criteriaTypeTen = {
                        criteriaLevel3Id: ctx.currentCriteriaLevel3Id,
                        criteriaLevel2Id: criteriaLevel2Id,
                        type: ctx.selectedCriteria.type,
                        isScreenableCriteriaItems: isScreenableItems,
                        isNotScreenableCriteriaItems: isNotScreenableCriteriaItems
                    };
                    ctx.listSelectedFilterCriteria = ctx.pushUniqueItems(ctx.listSelectedFilterCriteria, criteriaTypeTen, "criteriaLevel3Id");
                    break;
                case 16:
                    var isNotScreenableCriteriaItems = new Array();
                    for (var i = 0; i < ctx.selectedCriteria.type16IsNotScreenableCriteriaItems.length; i++) {
                        var copy = ctx.clone(ctx.selectedCriteria.type16IsNotScreenableCriteriaItems[i]);
                        isNotScreenableCriteriaItems.push(copy);
                    }

                    var criteriaType16: any = {
                        criteriaLevel3Id: ctx.currentCriteriaLevel3Id,
                        criteriaLevel2Id: criteriaLevel2Id,
                        id: ctx.selectedCriteria.id || ctx.selectedCriteria.type16IsNotScreenableCriteriaItems[0].id,
                        name: ctx.selectedCriteria.name,
                        type: ctx.selectedCriteria.type,
                        isNotScreenableCriteriaItems: isNotScreenableCriteriaItems
                    };
                    if (!ctx.isReport && ctx.selectedCriteria.isScreenable) {
                        criteriaType16.minValue = ctx.criteriaType16Chart.minValue;
                        criteriaType16.maxValue = ctx.criteriaType16Chart.maxValue;
                    } else {
                        criteriaType16.removed = true;
                        criteriaType16.isDataPoint = true;
                    }
                    ctx.listSelectedFilterCriteria = ctx.pushUniqueItems(ctx.listSelectedFilterCriteria, criteriaType16, "criteriaLevel3Id");
                    break;
                case 17:
                    var isScreenableItems = new Array();
                    for (var i = 0; i < ctx.selectedCriteria.type17IsScreenableCriteriaItems.length; i++) {
                        if (ctx.selectedCriteria.type17IsScreenableCriteriaItems[i].score >= 0)
                            isScreenableItems.push(ctx.selectedCriteria.type17IsScreenableCriteriaItems[i]);
                    }

                    var isNotScreenableCriteriaItems = new Array();
                    for (var i = 0; i < ctx.selectedCriteria.type17IsNotScreenableCriteriaItems.length; i++) {
                        var copy = ctx.clone(ctx.selectedCriteria.type17IsNotScreenableCriteriaItems[i]);
                        isNotScreenableCriteriaItems.push(copy);
                    }

                    var criteriaType17 = {
                        criteriaLevel3Id: ctx.currentCriteriaLevel3Id,
                        criteriaLevel2Id: criteriaLevel2Id,
                        type: ctx.selectedCriteria.type,
                        isScreenableCriteriaItems: isScreenableItems,
                        isNotScreenableCriteriaItems: isNotScreenableCriteriaItems
                    };
                    ctx.listSelectedFilterCriteria = ctx.pushUniqueItems(ctx.listSelectedFilterCriteria, criteriaType17, "criteriaLevel3Id");
                    break;
                case 25:
                    var isScreenableItems = new Array();
                    for (var i = 0; i < ctx.selectedCriteria.type25IsScreenableCriteriaItems.length; i++) {
                        if (ctx.scoreIsSelected(ctx.selectedCriteria.type25IsScreenableCriteriaItems[i].score, 25)) {
                            var copy = ctx.clone(ctx.selectedCriteria.type25IsScreenableCriteriaItems[i]);
                            copy.score = copy.answerCategories[copy.score].score;
                            isScreenableItems.push(copy);
                        }
                    }
                    var isNotScreenableCriteriaItems = new Array();
                    for (var i = 0; i < ctx.selectedCriteria.type25IsNotScreenableCriteriaItems.length; i++) {
                        var copy = ctx.clone(ctx.selectedCriteria.type25IsNotScreenableCriteriaItems[i]);
                        isNotScreenableCriteriaItems.push(copy);
                    }
                    var criteriaType25 = {
                        criteriaLevel3Id: ctx.currentCriteriaLevel3Id,
                        criteriaLevel2Id: criteriaLevel2Id,
                        type: ctx.selectedCriteria.type,
                        isScreenableCriteriaItems: isScreenableItems,
                        isNotScreenableCriteriaItems: isNotScreenableCriteriaItems
                    };
                    ctx.listSelectedFilterCriteria = ctx.pushUniqueItems(ctx.listSelectedFilterCriteria, criteriaType25, "criteriaLevel3Id");
                    break;
            }
        }

        public addCriteriaToFilter() {
            var ctx = this;
            //ctx.setIsModified(true);

            var newList = new Array();
            for (var i = 0; i < ctx.listSelectedFilterCriteria.length; i++) {
                var currentItem = angular.toJson(ctx.listSelectedFilterCriteria[i]);
                newList.push(currentItem);
            }
            ctx.$rootScope['loading'] = true;
            ctx.filterCriteriaService.addFilterCriteriaToFilter(ctx.filterId, newList)
                .success(() => {
                ctx.$rootScope['loading'] = false;
                ctx.notificationFactory.success();
                ctx.selectedCriteria.loading = false;
                ctx.closeDialog(true);
            })
                .error((data, status) => {
                ctx.$rootScope['loading'] = false;
                if (data["details"].length > 0) {
                    for (var i = 0; i < data["details"].length; i++) {
                        ctx.deleteSelectedFilterCriteriaById(data["details"][i].templateCriteriaId, data["details"][i].criteriaType);
                    }
                }
                ctx.notificationFactory.error("Error", "Not all selected criteria were inserted! Please check the selected criteria list for details.");
            });
        }
        //#endregion

        //delete only from array (not db)
        public deleteSelectedFilterCriteriaById(filterCriteriaId: string, criteriaType: number) {
            var ctx = this;
            for (var i = 0; i < ctx.listSelectedFilterCriteria.length; i++) {
                if (criteriaType == 1 || criteriaType == 3 || criteriaType == 6) {
                    if (ctx.listSelectedFilterCriteria[i].id == filterCriteriaId) {
                        ctx.listSelectedFilterCriteria.splice(i, 1);
                        return;
                    }
                }

                if (criteriaType == 4) {
                    if (ctx.listSelectedFilterCriteria[i].selectedCategories != null) {
                        for (var j = 0; j < ctx.listSelectedFilterCriteria[i].selectedCategories.length; j++) {
                            if (ctx.listSelectedFilterCriteria[i].selectedCategories[j].id == filterCriteriaId) {
                                ctx.listSelectedFilterCriteria[i].selectedCategories.splice(j, 1);
                                return;
                            }
                        }
                    }
                }

                if (criteriaType == 6 || criteriaType == 16) {
                    if (ctx.listSelectedFilterCriteria[i].id == filterCriteriaId) {
                        ctx.listSelectedFilterCriteria[i].removed = true;
                        return;
                    }
                }

                if (ctx.listSelectedFilterCriteria[i].isNotScreenableCriteriaItems != null) {
                    for (var j = 0; j < ctx.listSelectedFilterCriteria[i].isNotScreenableCriteriaItems.length; j++) {
                        if (ctx.listSelectedFilterCriteria[i].isNotScreenableCriteriaItems[j].id == filterCriteriaId) {
                            ctx.listSelectedFilterCriteria[i].isNotScreenableCriteriaItems.splice(j, 1);
                            return;
                        }
                    }
                }

                if (ctx.listSelectedFilterCriteria[i].isScreenableCriteriaItems != null) {
                    for (var j = 0; j < ctx.listSelectedFilterCriteria[i].isScreenableCriteriaItems.length; j++) {
                        if (ctx.listSelectedFilterCriteria[i].isScreenableCriteriaItems[j].id == filterCriteriaId) {
                            ctx.listSelectedFilterCriteria[i].isScreenableCriteriaItems.splice(j, 1);
                            return;
                        }
                    }
                }
            }
        }

        //#region Reports

        //used for Reports when you check Select All button
        public isSelectAllValid() {
            var ctx = this;
            var returnValue = false;
            if (ctx.listChecks != null) {
                for (var j = 0; j < ctx.listChecks.length; j++) {
                    if (ctx.listChecks[j].isOn == true) {
                        returnValue = true;
                        return returnValue;
                    }
                }
            }
            return returnValue;
        }

        //when you check select all from Reports - load checks
        public selectAllTemplateCriteriaLoad(tab: CriteriaTabs) {
            var ctx = this;

            ctx.showCriteriaLevel3 = false;
            ctx.showTemplateCriteria = true;
            ctx.listChecks = new Array();
            if (!ctx.isSelectAllChecked) {
                ctx.displayCriteriaLevel3(tab);
                return;
            }

            ctx.templateCriteriaService.listTemplateCriteriaByLevel3Id(tab.baseCriteria[0]["baseCriteria"][0].id)
                .then(templateCriteriaDto => {
                ctx.criteriaTypeByCriteriaLevel3 = templateCriteriaDto["data"][0].criteriaType;

                var enabledCriteria = templateCriteriaDto["data"].slice();
                if (enabledCriteria.length === 0) {
                    return;
                }
                ctx.isRiskRating = tab.baseCriteria[0]["baseCriteria"][0].name.indexOf("ESG Risk Rating") !== -1;

                if (ctx.criteriaTypeByCriteriaLevel3 === 10) {
                    ctx.listChecks = ctx.pushUniqueItems(ctx.listChecks, { name: "Value", isOn: false }, "name");
                } else if (ctx.criteriaTypeByCriteriaLevel3 === 16 && templateCriteriaDto["data"][0].name.startsWith("CG - Rank")) {
                    ctx.listChecks = ctx.pushUniqueItems(ctx.listChecks, { name: "CG - Rank", isOn: false }, "name");
                } else if (ctx.criteriaTypeByCriteriaLevel3 === 16 && templateCriteriaDto["data"][0].name.startsWith("CG - Percentile")) {
                    ctx.listChecks = ctx.pushUniqueItems(ctx.listChecks, { name: "CG - Percentile", isOn: false }, "name").slice();
                } else if (ctx.criteriaTypeByCriteriaLevel3 === 6 && ctx.isRiskRating)   {
                    ctx.listChecks = ctx.pushUniqueItems(ctx.listChecks, { name: "Value", isOn: false }, "name").slice();
                } else {
                    for (var i = 0; i < enabledCriteria.length; i++) {
                        var list = templateCriteriaDto["data"][i].name.split("-");
                        var name = list.length > 1 ? list[list.length - 1] : "Value";
                        var itemToCheck = { name: name, isOn: false }
                        ctx.listChecks = ctx.pushUniqueItems(ctx.listChecks, itemToCheck, "name");
                    }
                }
            });
        }

        //get the datapoints for checks that are on
        public selectAllDataPointsForReport(criteriaLevel2Id: string) {
            var ctx = this;
            ctx.templateCriteriaService.loadDataPointsForReport(criteriaLevel2Id, ctx.listChecks)
                .success((listTemplateCriteria: TemplateCriteriaDto[]) => {
                var groupByCriteriaLevel3IdList = ctx.groupBy(listTemplateCriteria, function (item) { return [item.criteriaLevel3Id] });

                for (var i = 0; i < groupByCriteriaLevel3IdList.length; i++) {
                    var filterGroupList = new Array();
                    for (var j = 0; j < groupByCriteriaLevel3IdList[i].length; j++) {
                        var claimValue;
                        
                        if (groupByCriteriaLevel3IdList[i][j].criteriaType === 4) {
                            claimValue = groupByCriteriaLevel3IdList[i][j].criteriaEntityField.split(" ")[0].toLowerCase();
                            if (this.claimsAuthorizationService.Validate("api/v1/pi", "POST", [{ key: 'pi', value: claimValue }])) {
                                var isOn = false;

                                var list = groupByCriteriaLevel3IdList[i][j].name.split("-");
                                var name = list.length > 1 ? list[list.length - 1] : "Value";

                                for (var k = 0; k < ctx.listChecks.length; k++) {
                                    if (ctx.listChecks[k].isOn && name == ctx.listChecks[k].name) {
                                        isOn = true;
                                    }
                                }

                                var critriaType4 = {
                                    criteriaLevel3Id: groupByCriteriaLevel3IdList[i][j].criteriaLevel3Id,
                                    criteriaLevel2Id: criteriaLevel2Id,
                                    id: groupByCriteriaLevel3IdList[i][j].id,
                                    name: groupByCriteriaLevel3IdList[i][j].name,
                                    isOn: isOn,
                                    isDataPoint: true,
                                    type: ctx.criteriaTypeByCriteriaLevel3,
                                    //claimSymbol: claimValue
                                };
                                if (ctx.criteriaTypeByCriteriaLevel3 == 1 || ctx.criteriaTypeByCriteriaLevel3 == 6) {
                                    ctx.listSelectedFilterCriteria = ctx.pushUniqueItems(ctx.listSelectedFilterCriteria, critriaType4, "criteriaLevel3Id");
                                } else {
                                    filterGroupList.push(critriaType4);
                                }
                            }
                        } else if (groupByCriteriaLevel3IdList[i][j].criteriaType === 10) {
                            // select all for criteria type 10
                            var item = {
                                criteriaLevel3Id: groupByCriteriaLevel3IdList[i][j].criteriaLevel3Id,
                                criteriaLevel2Id: criteriaLevel2Id,
                                id: groupByCriteriaLevel3IdList[i][j].id,
                                name: groupByCriteriaLevel3IdList[i][j].name,
                                isOn: true,
                                isDataPoint: true,
                                type: ctx.criteriaTypeByCriteriaLevel3,
                            };
                            if (ctx.criteriaTypeByCriteriaLevel3 == 1 || ctx.criteriaTypeByCriteriaLevel3 == 3 || ctx.criteriaTypeByCriteriaLevel3 == 6) {
                                ctx.listSelectedFilterCriteria = ctx.pushUniqueItems(ctx.listSelectedFilterCriteria, item, "criteriaLevel3Id");
                            } else {
                                filterGroupList.push(item);
                            }
                        } else if (groupByCriteriaLevel3IdList[i][j].criteriaType === 0 && ctx.isRiskRating) {

                            var templateCriteriaName = groupByCriteriaLevel3IdList[i][j].name;
                            if (templateCriteriaName.indexOf("Weight") == -1 && templateCriteriaName.indexOf("Sub-Industry Exposure Score") == -1) {
                                var item = {
                                    criteriaLevel3Id: groupByCriteriaLevel3IdList[i][j].criteriaLevel3Id,
                                    criteriaLevel2Id: criteriaLevel2Id,
                                    id: groupByCriteriaLevel3IdList[i][j].id,
                                    name: groupByCriteriaLevel3IdList[i][j].name,
                                    isOn: true,
                                    isDataPoint: true,
                                    type: ctx.criteriaTypeByCriteriaLevel3,
                                };
                                if (ctx.criteriaTypeByCriteriaLevel3 == 1 || ctx.criteriaTypeByCriteriaLevel3 == 3 || ctx.criteriaTypeByCriteriaLevel3 == 6) {
                                    ctx.listSelectedFilterCriteria = ctx.pushUniqueItems(ctx.listSelectedFilterCriteria, item, "criteriaLevel3Id");
                                } else {
                                    filterGroupList.push(item);
                                }
                            }
                        }
                        else {
                            // select all for other non-PI criterias
                            var templateCriteriaName = groupByCriteriaLevel3IdList[i][j].name;

                            var criteriaIsValid = true;
                            if (templateCriteriaName.toLowerCase() === 'sedol' || templateCriteriaName.toLowerCase() === 'wkn' || templateCriteriaName.toLowerCase() === 'valor')
                                criteriaIsValid = ctx.validateCriteria(templateCriteriaName.toLowerCase());

                            var isOn = ctx.listChecks
                                .filter(item => item.isOn)
                                .map(item => item.name)
                                .filter(dataTypeName => dataTypeName === "Value" || templateCriteriaName.startsWith(dataTypeName + " - ") || templateCriteriaName.endsWith("-" + dataTypeName))
                                .length > 0;

                            var item = {
                                criteriaLevel3Id: groupByCriteriaLevel3IdList[i][j].criteriaLevel3Id,
                                criteriaLevel2Id: criteriaLevel2Id,
                                id: groupByCriteriaLevel3IdList[i][j].id,
                                name: groupByCriteriaLevel3IdList[i][j].name,
                                isOn: isOn,
                                isDataPoint: true,
                                type: ctx.criteriaTypeByCriteriaLevel3
                            };
                            if (ctx.criteriaTypeByCriteriaLevel3 === 1) {
                                ctx.listSelectedFilterCriteria = ctx.pushUniqueItems(ctx.listSelectedFilterCriteria, item, "criteriaLevel3Id");
                            } else {
                                if (criteriaIsValid)
                                    filterGroupList.push(item);
                            }
                        }
                        if (filterGroupList.length > 0) {
                            var finalItem = {
                                criteriaLevel3Id: filterGroupList[0].criteriaLevel3Id,
                                criteriaLevel2Id: criteriaLevel2Id,
                                type: ctx.criteriaTypeByCriteriaLevel3,
                                isScreenableCriteriaItems: null,
                                isNotScreenableCriteriaItems: filterGroupList,
                                claimSymbol: claimValue
                            };                            
                            ctx.listSelectedFilterCriteria = ctx.pushUniqueItems(ctx.listSelectedFilterCriteria, finalItem, "criteriaLevel3Id");
                        }
                    }
                }
            });
        }

        public setDataPointOff(dataPoint: any) {
            dataPoint.isOn = false;
        }

        public setDataPointOn(dataPoint: any) {
            dataPoint.isOn = true;
        }
        //#endregion

        //#region updateCriteria
        //get filter criterias for the same group (with the same filterGroupId)
        public getSelectedCriteriaByFilterCriteriaId(filterCriteriaId: string) {
            var ctx = this;
            var promise = ctx.filterCriteriaService.getSelectedCriteriaByFilterCriteriaId(ctx.filterId, filterCriteriaId);
            promise.then(
                function (data) {
                    ctx.selectedCriteria = data;
                    ctx.updateCriteria(ctx.selectedCriteria);
                });
        }

        //load html for editing the filterCriteria
        public updateCriteria = function (selectedCriteria: SelectedCriteriaDto) {
            var ctx = this;

            ctx.activateTabForBaseCriteriaName(selectedCriteria.criteriaLevel3Id);
            ctx.activateBaseCriteria(selectedCriteria.name);
            ctx.showCriteriaLevel3 = false;
            ctx.showTemplateCriteria = true;

            switch (selectedCriteria.type) {
                case 1:
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType1.tpl.html";
                    ctx.templateCriteriaService.loadCriteriaTypeOne(selectedCriteria.id)
                        .success((criteriaTypeOne: CriteriaTypeOne) => {
                        ctx.currentCriteriaTypeOne = criteriaTypeOne;

                        ctx.criteriaTypeOneChart = {};

                        var yAxis = [];
                        ctx.criteriaTypeOneChart.xAxis = [];
                        ctx.currentCriteriaTypeOne.criteriaData.forEach(function (item) {
                            ctx.criteriaTypeOneChart.xAxis.push(item.xAxisValue);
                            yAxis.push(item.yAxisValue);
                        });

                        var data = {
                            labels: ctx.criteriaTypeOneChart.xAxis,
                            datasets: [
                                {
                                    label: "Companies",
                                    fillColor: "rgba(220,220,220,0.5)",
                                    strokeColor: "rgba(220,220,220,0.8)",
                                    highlightFill: "rgba(220,220,220,0.75)",
                                    highlightStroke: "rgba(220,220,220,1)",
                                    data: yAxis
                                }
                            ]
                        };

                        var options = {
                            scaleBeginAtZero: true,
                            showTooltips: false,
                            scaleShowGridLines: false,
                            scaleGridLineColor: "rgba(0,0,0,0)",
                            barValueSpacing: 12
                        };

                        ctx.criteriaTypeOneChart.minValue = selectedCriteria.typeOneMinValue;
                        ctx.criteriaTypeOneChart.maxValue = selectedCriteria.typeOneMaxValue;
                        ctx.criteriaTypeOneChart.data = data;
                        ctx.criteriaTypeOneChart.options = options;
                        ctx.criteriaTypeOneChart.invalidInput = ctx.chartInvalidInput(ctx.criteriaTypeOneChart);
                    })
                        .error((data, status, headers, config, statusText) => {
                        ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                    });
                    break;
                case 2:
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType2.tpl.html";
                    break;
                case 3:
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType3.tpl.html";

                    ctx.templateCriteriaService.loadCriteriaTypeThree(selectedCriteria.id, 0, 20)
                        .success((criteriaTypeThree: CriteriaTypeThree) => {
                        ctx.currentCriteriaTypeThree = criteriaTypeThree;
                        ctx.selectedCriteria.isScreenable = true;//criteriaTypeThree.isScreenable;
                        ctx.selectedCriteria.typeThreeSelectedItems = selectedCriteria.typeThreeSelectedItems;
                    })
                        .error((data, status, headers, config, statusText) => {
                        ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                    });
                    break;
                case 4:
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType4.tpl.html";

                    var selectedValues = [-1, 50, 25, 10, 5, 0];
                    ctx.selectedCriteria.typeFourCategories.forEach(category => {
                        var value = category.selectedValue;
                        if (value >= 0) {
                            category.selectedValue = selectedValues.indexOf(value);
                        }
                        else
                            category.selectedValue = -1;
                    });
                    break;
                case 5:
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType5.tpl.html";
                    ctx.templateCriteriaService.loadCriteriaTypeFive(selectedCriteria.id).success((criteriaTypeFive: CriteriaTypeFive) => {
                        ctx.currentCriteriaTypeFive = criteriaTypeFive;
                    }).error((data, status, headers, config, statusText) => {
                        ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                    });
                    break;
                case 25:
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType25.tpl.html";
                    ctx.templateCriteriaService.loadCriteriaType25(selectedCriteria.id).success((criteriaType25: CriteriaType25) => {
                        ctx.currentCriteriaType25 = criteriaType25;
                    }).error((data, status, headers, config, statusText) => {
                        ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                    });
                    break;
                case 6:
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType6.tpl.html";
                    ctx.templateCriteriaService.loadCriteriaTypeSix(selectedCriteria.id).success((criteriaTypeSix: CriteriaTypeSix) => {
                        ctx.currentCriteriaTypeSix = criteriaTypeSix;

                        if (selectedCriteria.typeSixMaxValue > criteriaTypeSix.maxValue || selectedCriteria.typeSixMaxValue == 0)
                            selectedCriteria.typeSixMaxValue = criteriaTypeSix.maxValue;

                        if (selectedCriteria.typeSixMinValue < criteriaTypeSix.minValue)
                            selectedCriteria.typeSixMinValue = criteriaTypeSix.minValue;

                        ctx.criteriaTypeSixChart = {};

                        var yAxis = [];
                        ctx.criteriaTypeSixChart.xAxis = [];
                        ctx.currentCriteriaTypeSix.criteriaData.forEach(function (item) {
                            ctx.criteriaTypeSixChart.xAxis.push(item.xAxisValue);
                            yAxis.push(item.yAxisValue);
                        });

                        var data = {
                            labels: ctx.criteriaTypeSixChart.xAxis,
                            datasets: [{
                                label: "Companies",
                                fillColor: "rgba(220,220,220,0.5)",
                                strokeColor: "rgba(220,220,220,0.8)",
                                highlightFill: "rgba(220,220,220,0.75)",
                                highlightStroke: "rgba(220,220,220,1)",
                                data: yAxis
                            }]
                        };

                        var options = {
                            scaleBeginAtZero: true,
                            showTooltips: false,
                            scaleShowGridLines: false,
                            scaleGridLineColor: "rgba(0,0,0,0)",
                            barValueSpacing: 7
                        };

                        ctx.criteriaTypeSixChart.minValue = selectedCriteria.typeSixMinValue;
                        ctx.criteriaTypeSixChart.maxValue = selectedCriteria.typeSixMaxValue;
                        ctx.criteriaTypeSixChart.data = data;
                        ctx.criteriaTypeSixChart.options = options;
                        ctx.criteriaTypeSixChart.invalidInput = ctx.chartInvalidInput(ctx.criteriaTypeSixChart);
                    }).error((data, status, headers, config, statusText) => {
                        ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                    });
                    break;
                case 7:
                    ctx.selectedCriteria.collectionItemsTypeSeven = ['0', '1', '2'];
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType7.tpl.html";
                    break;
                case 8:
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType8.tpl.html";
                    break;
                case 9:
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType9.tpl.html";
                    break;
                case 10:
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType10.tpl.html";
                    break;
                case 16:
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType16.tpl.html";
                    ctx.templateCriteriaService.loadCriteriaType16(selectedCriteria.id)
                        .success((criteriaType16: CriteriaType16) => {
                        ctx.currentCriteriaType16 = criteriaType16;

                        ctx.criteriaType16Chart = {};

                        var yAxis = [];
                        ctx.criteriaType16Chart.xAxis = [];
                        ctx.currentCriteriaType16.criteriaData.forEach(function (item) {
                            ctx.criteriaType16Chart.xAxis.push(item.xAxisValue);
                            yAxis.push(item.yAxisValue);
                        });

                        var data = {
                            labels: ctx.criteriaType16Chart.xAxis,
                            datasets: [{
                                label: "Companies",
                                fillColor: "rgba(220,220,220,0.5)",
                                strokeColor: "rgba(220,220,220,0.8)",
                                highlightFill: "rgba(220,220,220,0.75)",
                                highlightStroke: "rgba(220,220,220,1)",
                                data: yAxis
                            }]
                        };

                        var options = {
                            scaleBeginAtZero: true,
                            showTooltips: false,
                            scaleShowGridLines: false,
                            scaleGridLineColor: "rgba(0,0,0,0)",
                            barValueSpacing: 12
                        };

                        ctx.criteriaType16Chart.minValue = selectedCriteria.type16MinValue;
                        ctx.criteriaType16Chart.maxValue = selectedCriteria.type16MaxValue;
                        ctx.criteriaType16Chart.data = data;
                        ctx.criteriaType16Chart.options = options;
                        ctx.criteriaType16Chart.invalidInput = ctx.chartInvalidInput(ctx.criteriaType16Chart);
                    })
                        .error((data, status, headers, config, statusText) => {
                        ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                    });
                    break;
                case 17:
                    ctx.currentSelection = "Views/Tools/ScreeningTool/FilterCriteria/criteriaType17.tpl.html";
                    break;
                
                default:
                    ctx.currentSelection = "";
                    break;
            }

            ctx.selectedCriteria.loading = true;
            ctx.selectedCriteria.isUpdating = true;
            ctx.selectedFilterCriteria = selectedCriteria;
        };

        private chartInvalidInput(chart) {
            return () => {
                var ctx = this;

                var min = parseInt(chart.minValue);
                var max = parseInt(chart.maxValue);

                if (isNaN(min) || isNaN(max))
                    return true;

                var rangeMin = Math.min.apply(Math, chart.xAxis);
                var rangeMax = Math.max.apply(Math, chart.xAxis);

                if (min > max || rangeMin > min || rangeMax < max)
                    return true;

                return false;
            }
        }

        //send the request to the server and update
        public updateSelectedCriteria = function (criteriaLevel2Id: string) {
            var ctx = this;

            //ctx.setIsModified(true);

            if (ctx.selectedCriteria != null && ctx.selectedFilterCriteria.id != null) {
                switch (ctx.selectedFilterCriteria.type) {
                    case 1:
                        ctx.filterCriteriaService.addCriteriaTypeOne(ctx.filterId, criteriaLevel2Id, ctx.selectedCriteria.id, ctx.criteriaTypeOneChart.minValue, ctx.criteriaTypeOneChart.maxValue, true)
                            .success(() => {
                            ctx.notificationFactory.success();
                            ctx.selectedCriteria.loading = false;
                            ctx.closeDialog(true);
                        }).error((data, status, headers, config, statusText) => {
                            ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                        });
                        break;
                    case 2:
                        var isScreenableItems = new Array();
                        for (var i = 0; i < ctx.selectedCriteria.typeTwoIsScreenableCriteriaItems.length; i++) {
                            if (ctx.selectedCriteria.typeTwoIsScreenableCriteriaItems[i].score != 0)
                                isScreenableItems.push(ctx.selectedCriteria.typeTwoIsScreenableCriteriaItems[i]);
                        }

                        var isNotScreenableItems = new Array();
                        isNotScreenableItems = ctx.selectedCriteria.typeTwoIsNotScreenableCriteriaItems;
                        ctx.filterCriteriaService.addCriteriaTypeTwo(ctx.filterId, ctx.selectedCriteria.filterGroupId, criteriaLevel2Id, isScreenableItems, isNotScreenableItems, true)
                            .success(() => {
                            ctx.notificationFactory.success();
                            ctx.selectedCriteria.loading = false;
                            ctx.closeDialog(true);
                        }).error((data, status, headers, config, statusText) => {
                            ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                        });
                        break;
                    case 3:
                        var isScreenableItems = new Array();
                        ctx.selectedCriteria.typeThreeIsScreenableCriteriaItems[0].selectedItems = ctx.selectedCriteria.typeThreeSelectedItems;
                        isScreenableItems.push(ctx.selectedCriteria.typeThreeIsScreenableCriteriaItems[0]);
                        
                        var isNotScreenableItems = new Array();
                        isNotScreenableItems = ctx.selectedCriteria.typeThreeIsNotScreenableCriteriaItems;

                        ctx.filterCriteriaService.addCriteriaTypeThree(ctx.filterId, criteriaLevel2Id, ctx.selectedCriteria.id, isScreenableItems, isNotScreenableItems, ctx.selectedCriteria.filterGroupId, true)
                            .success(() => {
                            ctx.notificationFactory.success();
                            ctx.selectedCriteria.loading = false;
                            ctx.closeDialog(true);
                        })
                            .error((data, status, headers, config, statusText) => {
                            ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                            });
                        break;
                    case 4:
                        var selectedCategories = [];

                        ctx.selectedCriteria.typeFourCategories.forEach((category) => {
                            if (category.isInvolved || category.isSignificantOwnership || category.selectedValue > 0) {
                                if (category.selectedValue > 0) {
                                    selectedCategories.push(
                                        {
                                            categoryOfInvolvement: category.categoryOfInvolvement,
                                            isSignificantOwnership: false,
                                            isInvolvement: false,
                                            revenueRange: category.selectedValue > 0,
                                            start: category.revenueRange[5 - category.selectedValue].start,
                                            stop: category.revenueRange[5 - category.selectedValue].stop,
                                            name: category.name,
                                            categorySymbol: category.categorySymbol,
                                            id: category.templateCriteriaId
                                        });
                                }

                                if (category.isInvolved) {
                                    selectedCategories.push(
                                        {
                                            categoryOfInvolvement: category.categoryOfInvolvement,
                                            isSignificantOwnership: false,
                                            isInvolvement: category.isInvolved,
                                            revenueRange: false,
                                            start: 0,
                                            stop: 0,
                                            name: category.name,
                                            categorySymbol: category.categorySymbol,
                                            id: category.templateCriteriaId
                                        });
                                }

                                if (category.isSignificantOwnership) {
                                    selectedCategories.push(
                                        {
                                            categoryOfInvolvement: category.categoryOfInvolvement,
                                            isSignificantOwnership: category.isSignificantOwnership,
                                            isInvolvement: false,
                                            revenueRange: false,
                                            start: 0,
                                            stop: 0,
                                            name: category.name,
                                            categorySymbol: category.categorySymbol,
                                            id: category.templateCriteriaId
                                        });
                                }
                            }
                        });
                        var criteriaTypeFour = {
                            criteriaLevel3Id: ctx.currentCriteriaLevel3Id,
                            criteriaLevel2Id: criteriaLevel2Id,
                            id: ctx.selectedCriteria.id,
                            name: ctx.selectedCriteria.name,
                            type: ctx.selectedCriteria.type,
                            filterGroupId: ctx.selectedCriteria.filterGroupId,
                            selectedCategories: selectedCategories,
                            IsNotScreenableCriteriaItems: ctx.selectedCriteria.typeFourIsNotScreenableCriteriaItems,
                            claimSymbol: ctx.selectedCriteria.typeFourSymbol
                        }

                        ctx.filterCriteriaService.addCriteriaTypeFour(ctx.filterId, criteriaLevel2Id, ctx.selectedCriteria.filterGroupId, criteriaTypeFour, true)
                            .success(() => {
                            ctx.notificationFactory.success();
                            ctx.selectedCriteria.loading = false;
                            ctx.closeDialog(true);
                        })
                            .error((data, status, headers, config, statusText) => {
                            ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                        });
                        break;
                    case 5:
                        var isScreenableItems = new Array();
                        for (var i = 0; i < ctx.selectedCriteria.typeFiveIsScreenableCriteriaItems.length; i++) {
                            if (ctx.scoreIsSelected(ctx.selectedCriteria.typeFiveIsScreenableCriteriaItems[i].score, 5))
                                isScreenableItems.push(ctx.selectedCriteria.typeFiveIsScreenableCriteriaItems[i]);
                        }

                        var isNotScreenableItems = new Array();
                        isNotScreenableItems = ctx.selectedCriteria.typeFiveIsNotScreenableCriteriaItems;

                        ctx.filterCriteriaService.addCriteriaTypeFive(ctx.filterId, ctx.selectedCriteria.filterGroupId, criteriaLevel2Id, isScreenableItems, isNotScreenableItems, true)
                            .success(() => {
                            ctx.notificationFactory.success();
                            ctx.selectedCriteria.loading = false;
                            ctx.closeDialog(true);
                        }).error((data, status, headers, config, statusText) => {
                            ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                        });
                        break;
                    case 6:
                        var isNotScreenableItems = new Array();
                        isNotScreenableItems = ctx.selectedCriteria.typeSixIsNotScreenableCriteriaItems;
                        ctx.filterCriteriaService.addCriteriaTypeSix(ctx.filterId, ctx.selectedCriteria.filterGroupId, criteriaLevel2Id, ctx.selectedCriteria.id, ctx.criteriaTypeSixChart.minValue, ctx.criteriaTypeSixChart.maxValue, isNotScreenableItems, true)
                            .success(() => {
                                ctx.notificationFactory.success();
                                ctx.selectedCriteria.loading = false;
                                ctx.closeDialog(true);
                            }).error((data, status, headers, config, statusText) => {
                                ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                            });
                        break;
                    case 7:
                        var isScreenableItems = new Array();
                        ctx.selectedCriteria.typeSevenIsScreenableCriteriaItems[0].selectedItems = ctx.selectedCriteria.typeSevenSelectedItems;
                        isScreenableItems.push(ctx.selectedCriteria.typeSevenIsScreenableCriteriaItems[0]);

                        var isNotScreenableItems = new Array();
                        isNotScreenableItems = ctx.selectedCriteria.typeSevenIsNotScreenableCriteriaItems;

                        ctx.filterCriteriaService.addCriteriaTypeSeven(ctx.filterId, ctx.selectedCriteria.filterGroupId, criteriaLevel2Id, isScreenableItems, isNotScreenableItems, true)
                            .success(() => {
                            ctx.notificationFactory.success();
                            ctx.selectedCriteria.loading = false;
                            ctx.closeDialog(true);
                        })
                            .error((data, status, headers, config, statusText) => {
                            ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                        });

                        break;
                    case 8:
                        var isScreenableItems = new Array();
                        for (var i = 0; i < ctx.selectedCriteria.typeEightIsScreenableCriteriaItems.length; i++) {
                            if (ctx.selectedCriteria.typeEightIsScreenableCriteriaItems[i].score != 0)
                                isScreenableItems.push(ctx.selectedCriteria.typeEightIsScreenableCriteriaItems[i]);
                        }

                        var isNotScreenableItems = new Array();
                        isNotScreenableItems = ctx.selectedCriteria.typeEightIsNotScreenableCriteriaItems;
                        ctx.filterCriteriaService.addCriteriaTypeEight(ctx.filterId, ctx.selectedCriteria.filterGroupId, criteriaLevel2Id, isScreenableItems, isNotScreenableItems, true)
                            .success(() => {
                            ctx.notificationFactory.success();
                            ctx.selectedCriteria.loading = false;
                            ctx.closeDialog(true);
                        }).error((data, status, headers, config, statusText) => {
                            ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                        });
                        break;
                    case 9:
                        var isScreenableItems = new Array();
                        for (var i = 0; i < ctx.selectedCriteria.typeNineIsScreenableCriteriaItems.length; i++) {
                            if (ctx.selectedCriteria.typeNineIsScreenableCriteriaItems[i].score != 0)
                                isScreenableItems.push(ctx.selectedCriteria.typeNineIsScreenableCriteriaItems[i]);
                        }

                        var isNotScreenableItems = new Array();
                        isNotScreenableItems = ctx.selectedCriteria.typeNineIsNotScreenableCriteriaItems;
                        ctx.filterCriteriaService.addCriteriaTypeNine(ctx.filterId, ctx.selectedCriteria.filterGroupId, criteriaLevel2Id, isScreenableItems, isNotScreenableItems, true)
                            .success(() => {
                            ctx.notificationFactory.success();
                            ctx.selectedCriteria.loading = false;
                            ctx.closeDialog(true);
                        }).error((data, status, headers, config, statusText) => {
                            ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                        });
                        break;
                    case 10:
                        var isScreenableItems = new Array();
                        //for (var i = 0; i < ctx.selectedCriteria.typeTenIsScreenableCriteriaItems.length; i++) {
                        //    if (ctx.selectedCriteria.typeTenIsScreenableCriteriaItems[i].score != 0)
                        //        isScreenableItems.push(ctx.selectedCriteria.typeTenIsScreenableCriteriaItems[i]);
                        //}

                        var isNotScreenableItems = new Array();
                        isNotScreenableItems = ctx.selectedCriteria.typeTenIsNotScreenableCriteriaItems;
                        ctx.filterCriteriaService.addCriteriaTypeTen(ctx.filterId, ctx.selectedCriteria.filterGroupId, criteriaLevel2Id, isScreenableItems, isNotScreenableItems, true)
                            .success(() => {
                            ctx.notificationFactory.success();
                            ctx.selectedCriteria.loading = false;
                            ctx.closeDialog(true);
                        }).error((data, status, headers, config, statusText) => {
                            ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                        });
                        break;
                    case 16:
                        var isNotScreenableItems = new Array();
                        isNotScreenableItems = ctx.selectedCriteria.type16IsNotScreenableCriteriaItems;
                        ctx.filterCriteriaService.addCriteriaType16(ctx.filterId, ctx.selectedCriteria.filterGroupId, criteriaLevel2Id, ctx.selectedCriteria.id, ctx.criteriaType16Chart.minValue, ctx.criteriaType16Chart.maxValue, isNotScreenableItems, true)
                            .success(() => {
                            ctx.notificationFactory.success();
                            ctx.selectedCriteria.loading = false;
                            ctx.closeDialog(true);
                        }).error((data, status, headers, config, statusText) => {
                            ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                        });
                        break;
                    case 17:
                        var isScreenableItems = new Array();
                        for (var i = 0; i < ctx.selectedCriteria.type17IsScreenableCriteriaItems.length; i++) {
                            if (ctx.selectedCriteria.type17IsScreenableCriteriaItems[i].score >= 0)
                                isScreenableItems.push(ctx.selectedCriteria.type17IsScreenableCriteriaItems[i]);
                        }

                        var isNotScreenableItems = new Array();
                        isNotScreenableItems = ctx.selectedCriteria.type17IsNotScreenableCriteriaItems;
                        ctx.filterCriteriaService.addCriteriaType17(ctx.filterId, ctx.selectedCriteria.filterGroupId, criteriaLevel2Id, isScreenableItems, isNotScreenableItems, true)
                            .success(() => {
                            ctx.notificationFactory.success();
                            ctx.selectedCriteria.loading = false;
                            ctx.closeDialog(true);
                        }).error((data, status, headers, config, statusText) => {
                            ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                        });
                        break;
                    case 25:
                        var isScreenableItems = new Array();
                        for (var i = 0; i < ctx.selectedCriteria.type25IsScreenableCriteriaItems.length; i++) {
                            if (ctx.scoreIsSelected(ctx.selectedCriteria.type25IsScreenableCriteriaItems[i].score, 25)) {
                                var copy = ctx.clone(ctx.selectedCriteria.type25IsScreenableCriteriaItems[i]);
                                copy.score = copy.answerCategories[copy.score].score;
                                isScreenableItems.push(copy);
                            }
                        }

                        var isNotScreenableItems = new Array();
                        isNotScreenableItems = ctx.selectedCriteria.type25IsNotScreenableCriteriaItems;

                        ctx.filterCriteriaService.addCriteriaType25(ctx.filterId, ctx.selectedCriteria.filterGroupId, criteriaLevel2Id, isScreenableItems, isNotScreenableItems, true)
                            .success(() => {
                                ctx.notificationFactory.success();
                                ctx.selectedCriteria.loading = false;
                                ctx.closeDialog(true);
                            }).error((data, status, headers, config, statusText) => {
                                ctx.notificationFactory.error("Error", ctx.staticObjects.getErrorMesageByStatusCode(status, data));
                            });
                        break;
                    default:
                        ctx.currentSelection = "";
                        break;
                }
            }
        };
        //#endregion

        //#region UI
        //used in UI to show and hide the template criteria aria or criteriaLevel3 area
        public displayCriteriaLevel3 = function (childTab) {
            var ctx = this;
            if (childTab != undefined || childTab != null) {
                ctx.selectedCriteria.isUpdating = false;
                ctx.selectedChildTab = childTab;
                ctx.isSelectAllChecked = false;
                ctx.listChecks = new Array();
                ctx.currentSelection = "";
                ctx.activateTabForChildTabName(childTab.name);

                ctx.isSelectAllDisabled = childTab.baseCriteria
                    .map(x => x.baseCriteria)
                    .reduce((a, b) => a.concat(b))
                    .filter(x => x.isEnabled)
                .length === 0;
            }
            ctx.showTemplateCriteria = false;
            ctx.showCriteriaLevel3 = true;
        }

        //used in UI to show and hide the template criteria aria or criteriaLevel3 area
        public resetCriteriaLevel3 = function () {
            var ctx = this;
            ctx.showCriteriaLevel3 = false;
            ctx.showTemplateCriteria = false;
        }
        //used in UI
        public removePrettyUnderline(childTab): string {
            return childTab.isEnabled ? "prettysparkles" : "disabled";
        }
        //#endregion

        //#region Validate

        public validateCriteria(shortName: string): boolean {
            var ctx = this;
            if (this.claimsAuthorizationService.Validate("api/v1/filters/{filterId}/criteria", "POST", [{ key: 'shortName', value: shortName }])) {
                return true;
            }

            return false;
        }

        public validateCriteriaForPi(shortName: string): boolean {
            var ctx = this;
            var convertedShortName = "";
            switch (shortName) {
                case "Adult Entertainment":
                    convertedShortName = "ae";
                    break;
                case "Alcoholic Beverages":
                    convertedShortName = "al";
                    break;
                case "Controversial Weapons":
                    convertedShortName = "cw";
                    break;
                case "Small Arms":
                    convertedShortName = "fa";
                    break;
                case "Fur and Specialty Leather":
                    convertedShortName = "fl";
                    break;
                case "Gambling":
                    convertedShortName = "gb";
                    break;
                case "Genetically Modified Plants and Seeds":
                    convertedShortName = "gm";
                    break;
                case "Military Contracting":
                    convertedShortName = "mc";
                    break;
                case "Nuclear":
                    convertedShortName = "np";
                    break;
                case "Pesticides":
                    convertedShortName = "pe";
                    break;
                case "Tobacco Products":
                    convertedShortName = "tp";
                    break;
                case "Abortion":
                    convertedShortName = "ab";
                    break;
                case "Contraceptives":
                    convertedShortName = "co";
                    break;
                case "Human Embryonic Stem Cell and Fetal Tissue":
                    convertedShortName = "sc";
                    break;
                case "Palm Oil":
                    convertedShortName = "pm";
                    break;
                case "Pork Products":
                    convertedShortName = "pp";
                    break;
                case "Animal Testing":
                    convertedShortName = "at";
                    break;
                case "Predatory Lending":
                    convertedShortName = "pl";
                    break;
                case "Thermal Coal":
                    convertedShortName = "tc";
                    break;
                case "Oil Sands":
                    convertedShortName = "os";
                    break;
                case "Arctic Oil & Gas Exploration":
                    convertedShortName = "ac";
                    break;
            }

            if (this.claimsAuthorizationService.Validate("api/v1/pi", "POST", [{ key: 'pi', value: convertedShortName }])) {
                return true;
            }

            return false;
        }

        public validateCriteriaForGccs(selection: string): boolean {
            var ctx = this;

            if (selection != undefined) {
                if (selection === "1")
                    if (this.claimsAuthorizationService.Validate("api/v1/filters/{filterId}/criteria", "POST", [{ key: 'shortName', value: 'gccsnc' }]))
                        return true;

                if (selection === "2")
                    if (this.claimsAuthorizationService.Validate("api/v1/filters/{filterId}/criteria", "POST", [{ key: 'shortName', value: 'gccswl' }]))
                        return true;
            } else {
                if (this.claimsAuthorizationService.Validate("api/v1/filters/{filterId}/criteria", "POST", [{ key: 'shortName', value: 'gccsnc' }]) ||
                    this.claimsAuthorizationService.Validate("api/v1/filters/{filterId}/criteria", "POST", [{ key: 'shortName', value: 'gccswl' }])) {
                    return true;
                }
            }
            return false;
        }
        //#endregion
    }

    // register controller with News Angular module
    angular.module(screeningToolModuleName).controller('EditFilterCriteriaController',
        ['$rootScope', '$scope', '$q', '$location', 'notificationFactory', 'staticObjects', 'screeningToolFactory', 'templateCriteriaService', 'FilterCriteriaService', '$modal', '$modalInstance', 'filterId', 'filterCriteriaId', 'isReport', 'ClaimsAuthorizationService', EditFilterCriteriaController]);
}
